I-PHP: Sika isicatshulwa sakho kwigama usebenzisa strrpos

Ikhowudi ye-html

Ukuba usebenza ne-PHP kwaye ufuna ukubonisa kuphela isicatshulwa kwisicatshulwa sakho somthombo kwaye usike kwinani labalinganiswa, isicatshulwa sakho sinokubonakala sibi ukuba senziwe phakathi komtya. Kwafuneka ndibhale umsebenzi wokukwenza oku kwi-ASP nakwi-ASP.NET ngokusisiseko ehamba ngebhayisikile ukusuka kumlinganiswa wokugqibela ukubuya ukuze ndifumane indawo yokugqibela ndiyinqumle apho. Uhlobo olubi kunye nentwana yokubulala ngaphezulu. Ungayibona isenzeka ekhayeni lam iphepha apho ndibonelela kuphela ngoonobumba bokuqala abangama-500.

Ndikulungele ngokupheleleyo ukuphuhlisa umsebenzi ofanayo ne-PHP namhlanje kodwa ndafumanisa (njengesiqhelo) ukuba i-PHP inomsebenzi osele uyenzile, izie.

Ikhowudi endala iya kuthatha umtya ($ umxholo) ukusuka kumlinganiswa wokuqala uye kwelona nani liphezulu labalinganiswa obafunayo ($ maxchars):

$ umxholo = substr (umxholo weedola, 0, ii-maxchars); echo umxholo weedola;

Ikhowudi entsha:

$ umxholo = substr (umxholo weedola, 0, ii-maxchars); $ pos = strrpos (umxholo weedola, ""); ukuba ($ pos> 0) {$ umxholo = substr (umxholo weedola, 0, $ pos); } echo umxholo weedola;

Ke ikhowudi entsha kuqala isika umxholo kumda womlinganiswa owufunayo. Nangona kunjalo, inyathelo elilandelayo kukujonga indawo yokugqibela ("") kumxholo. $ pos iya kuphelela ekubeni kweso sikhundla. Ngoku, ndiqinisekisa nje ukuba kukho isithuba kumxholo ngokubuza ukuba $ pos> 0. Ukuba akukho, izakusika umxholo kwinani labalinganiswa endibacelileyo. Ukuba kukho nasiphi na isithuba, uya kuwusika ngobumnandi umxholo wam esithubeni.

Le yindlela entle yokusebenzisa indibaniselwano yelona nani liphezulu labalinganiswa kunye nokusika kwilizwi. Ndiyathemba ukuba uyathanda!

Kwaye ndiqinisekile ukuba ndizakufumanisa ukuba kukho umsebenzi we-ASP.NET owenza oku… andifumananga.

7 Comments

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

    Ukuba i-$content iqale EMFUTSHANE kune $maxchars ikhowudi njengoko ibhaliwe isajonge ekunene ukuya ngasekhohlo kwisithuba kwaye usika igama lokugqibela. Ungadibanisa isithuba ekupheleni komxholo we-$, okanye wenze ukuba (strlen()...)

  5. 5

    Oku kubonakala kusebenza njengomsebenzi (ukujongana nezimvo zangaphambili):

    umsebenzi showexcerpt ($ umxholo, $ maxchars) {

    ukuba (strlen($ umxholo) > $ maxchars) {

    $umxholo= substr($content, 0, $maxchars);
    $pos = strrpos($umxholo, " ");

    ukuba ($pos>0) {
    Umxholo we-$ = substr (i-$ umxholo, i-0, i-$ pos);
    }

    buyisela umxholo we-$. "...";

    } Enye {

    ubuyisele umxholo wemali;

    }

    }

  6. 6

    Kuthekani ukuba umlinganiswa wethu wokugqibela usisiphumlisi esifana ne-stop, uphawu lwesikhuzo okanye uphawu lombuzo? Ngelishwa, le khowudi iyakulisula lonke igama elingaphambi kophawu lokubhala.  

    Ndicinga ukuba ungenza ngcono xa ubhala into eyomelele ngakumbi.

  7. 7

Ingaba ucinga ntoni?

Le sayithi isebenzisa i-Akismet ukunciphisa ugaxekile. Funda indlela idatha yakho yokubhaliweyo isetyenziswe ngayo.