PHP: ʻoki i kāu huaʻōlelo ma ka ʻōlelo ma ka hoʻohana ʻana i strrpos

code html

Inā ʻoe e hana nei me PHP a makemake ʻoe e hōʻike i kahi ʻāpana mai kāu ʻatikala kumu a ʻokiʻoki iā ia i nā huapalapala he nui, hiki i kāu huaʻōlelo ke ʻino inā hana ʻia ke aho waena. Ua pono wau e kākau i kahi hana e hana ai i kēia ma ASP a ma ASP.NET i holoholo maʻamau mai ke ʻano hope i hope e ʻimi i ka wahi hope loa a ʻoki iā ia ma laila. ʻAno maikaʻi ʻole a ʻano liʻiliʻi hoʻi o ka overkill. Hiki iā ʻoe ke ʻike i kēia i ka hana ma koʻu home palapala kahi e hāʻawi wale ai au i nā huapalapala 500 mua loa.

Ua mākaukau wau e hoʻomohala i ka hana like me PHP i kēia lā akā ua ʻike (e like me nā manawa maʻamau) he hana kā PHP e hana nei, strrpos.

Lawe ka pāʻālua kahiko i kahi substring (ʻikepili $) mai ka huapalapala mua a i ka helu nui o nā huapalapala āu e makemake ai ($ maxchars):

$ maʻiʻo = substr ($ ʻike, 0, $ maxchars); kumumanaʻo $ ʻike;

ʻO ke code hou:

$ maʻiʻo = substr ($ ʻike, 0, $ maxchars); $ pos = strrpos ($ maʻiʻo, ""); inā ($ pos> 0) {$ maʻiʻo = substr ($ ʻike, 0, $ pos); } echo $ ʻike;

No laila ke ʻoki mua nei ke code hou i ka ʻike ma ka palena o ke ʻano āu e ʻimi nei. Eia naʻe, ʻo ka hana hou e nānā no ka hakahaka hope ("") i ka ʻike. E ea ana ʻo $ pos i kēlā kūlana. I kēia manawa, e hōʻoiaʻiʻo wale nō wau aia kekahi wahi i ka ʻike ma ka noi ʻana inā $ pos> 0. Inā ʻaʻohe, e ʻoki wale nō ia i ka ʻike ma ka helu o nā huapalapala aʻu i noi ai. Inā loaʻa kekahi hakahaka, e ʻokiʻoki ia i kaʻu ʻike ma ia wahi.

He ala maikaʻi kēia o ka hoʻohana ʻana i ka hui pū ʻana o ka helu nui o nā huapalapala a me keʻokiʻoki ʻana iā ia ma ka ʻōlelo. Lana ka manaʻo makemake ʻoe!

A maopopo iaʻu e ʻike inā aia kahi hana ASP.NET e hana ai i kēia ... ʻaʻole hiki iaʻu ke loaʻa.

7 Comments

  1. 1
  2. 2
  3. 3
  4. 4

    Inā ʻoi loa ka ʻike o ka ʻike ma mua o $ maxchars ma mua o $ maxchars ke kahakaha e like me ke kākau ʻana e nānā pono ʻākau a hema no kahi ākea a ʻoki i ka huaʻōlelo hope loa. Hiki iā ʻoe ke concatenate i kahi i ka hopena o $ ʻike, a i ʻole hana i inā (strlen ()…)

  5. 5

    Me he hana lā kēia (e kamaʻilio ana i ka ʻōlelo ma mua):

    hana showexcerpt ($ ʻike, $ maxchars) {

    inā (strlen ($ maʻiʻo)> $ maxchars) {

    $ maʻiʻo = substr ($ ʻike, 0, $ maxchars);
    $ pos = strrpos ($ maʻiʻo, "");

    inā ($ pos> 0) {
    $ maʻiʻo = substr ($ ʻike, 0, $ pos);
    }

    hoʻihoʻi $ ʻike. "…";

    } ka mea'ē aʻe {

    hoʻihoʻi i ka helu $;

    }

    }

  6. 6

    He aha inā inā ʻo kā mākou huapalapala hope loa kahi ʻano kikoʻī e like me ke kikoʻī, kīkaha a i ʻole ka māka nīnau? Minamina, holoi kēia kuhi i ka huaʻōlelo āpau ma mua o ke ʻano o ka puana.  

    Manaʻo wau e ʻoi aku kou maikaʻi ma ke kākau ʻana i kahi mea i ʻoi iki ka ikaika.

  7. 7

Pehea kou manaʻo?

Ke hoʻohana nei kēia pūnaewele i ka Akismet e ho'ēmi i ka spam. E aʻo pehea e hanaʻia ai kāuʻikeʻikepili.