PHP: Cheka chidimbu chako pashoko uchishandisa strrpos

html kodhi

Kana iwe uri kushanda nePHP uye iwe uchingoda kuratidza chete chidimbu kubva kune yako sosi zvinyorwa uye wozvicheka pane akati wandei mavara, yako excerpt inogona kutaridzika yakashata kana yaitwa pakati-tambo. Ini ndaifanirwa kunyora basa kuti ndiite izvi muASP uye muASP.NET iyo yaichovha bhasikoro kubva kune wekupedzisira hunhu kumashure kuti ndiwane yekupedzisira nzvimbo ndocheka ipapo. Mhando yezvakaipa uye zvishoma zvekuwandisa. Unogona kunyatsoona izvi muchiita pamba pangu peji kwandinongopa mavara ekutanga mazana mashanu.

Ini ndanga ndakagadzirira zvizere kukudziridza zvakafanana basa nePHP nhasi asi ndawana (senguva dzose) kuti PHP yaive nebasa rinozviita, unoenda.

Iyo yekare kodhi inotora substring ($ zvemukati) kubva kune yekutanga hunhu kuenda kune yakanyanya nhamba ye mavara iwe aunoda ($ maxchars):

$ zvemukati = substr ($ zvemukati, 0, $ maxchars); echo $ zvemukati;

Iyo kodhi nyowani:

$ zvemukati = substr ($ zvemukati, 0, $ maxchars); $ pos = strrpos ($ zvemukati, ""); kana ($ pos> 0) {$ zvemukati = substr ($ zvemukati, 0, $ pos); } echo $ zvemukati;

Saka kodhi nyowani inotanga kucheka zvirimo pamuganhu wehunhu iwe wauri kutsvaga. Zvisinei, danho rinotevera ndere kutsvaga nzvimbo yekupedzisira ("") mune zvirimo. $ pos ichawedzera kuva chinzvimbo ichocho. Zvino, ini ndinongovimbisa kuti pane chaipo nzvimbo mune zvirimo nekubvunza kana $ pos> 0. Kana pasina, zvinongogura zvirimo panhamba yemavara andakumbira. Kana paine chero nzvimbo, inonyatso kucheka zvemukati zvangu panzvimbo.

Iyi inzira yakanaka yekushandisa kusanganiswa kwehuwandu hwehuwandu hwevatambi uye kucheka neshoko. Ndinovimba unoda!

Uye ndine chokwadi chekuti ndichawana kana paine ASP.NET basa rinoita izvi… ini handina kuwana imwe.

7 Comments

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

    Kana $content pekutanga SHORTER than $maxchars kodhi sezvakanyorwa icharamba yakatarisa kurudyi kuruboshwe kune imwe nzvimbo uye cheka izwi rekupedzisira. Unogona kubatanidza nzvimbo pakupera kwe$content, kana kuita kana (strlen()…)

  5. 5

    Izvi zvaiita sezvinoshanda sebasa (kutaura zvakataurwa yapfuura):

    basa showexcerpt ($ content, $ maxchars) {

    kana (strlen($ zvirimo)> $maxchars) {

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

    kana ($pos>0) {
    $ content = substr($ content, 0, $pos);
    }

    return $ content . "…";

    } Mumwe {

    dzoka $ zvakanakira;

    }

    }

  6. 6

    Ko kana hunhu hwedu hwekupedzisira ari hunhu hwepumisikidzo senge chimbomira, mucherechedzo wekudanidzira kana mubvunzo yemubvunzo? Nehurombo, iyi kodhi ichapukuta izwi rese rakatangira rakataurwa hunhu hwepunctuation.  

    Ndinofunga unenge uri nani pakunyora chimwe chinhu chakanyanya kusimba.

  7. 7

Unofungei?

Nzvimbo iyi inoshandisa Akismet kuderedza spam. Dzidza kuti sei deta yako inoshandiswa.