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 me 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 ua ʻoki mua 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 wau e ʻike inā aia kahi hana ASP.NET e hana ai i kēia ... ʻaʻole hiki iaʻu ke loaʻa.