php - String cut doesn't work perfectly -


currently use script:

$tstring = strip_tags($nstitle); if (strlen($tstring) > 65) {      // truncate string     $stringcut = substr($tstring, 0, 65);      // make sure ends in word assassinate doesn't become ass...     $tstring = substr($stringcut, 0, strrpos($stringcut, ' ')).                '.....<a href="">read more</a>';  } 

sometimes doesn't work if type 2 lines , use cut string. gives different results, like, instance, getting output every line different length. want same length of lines if same or not.

you can't make every input same length (65) unless add padding. since chopping off after last space, last space might occur @ different position in string. also, if there's no space, might not expected result. 1. check, , 2. pad.

    // truncate string     $stringcut = substr($tstring, 0, 65);     //make sure can find space     if (strrpos($stringcut, ' ') > 0) {         $stringcut = substr($stringcut, 0, strrpos($stringcut, ' '));     }     //then pad string 65 characters long     while (strlen($stringcut) < 65) {         $stringcut.="*";     }  // make sure ends in word assassinate doesn't become ass... $tstring = $stringcut . '.....<a href="">read more</a>'; 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -