hi,
I have some text which will return from DB, and I want to limit the string length to display, let say 50 character, I tried substr($string, 0, 50); but it may cut in the middle of the word, so I want to make sure it will cut at the space charactor, how can i do it? thanks
I would say do something like this:
$temp = substr($original,0,51); $lastSpace=strrpos($temp," "); if($lastSpace===false) { $final = substr($temp,0,50); } else { $final = substr($temp,0,$lastSpace); }
Good luck!
Originally posted by jcarver I would say do something like this: $temp = substr($original,0,51); $lastSpace=strrpos($temp," "); if($lastSpace===false) { $final = substr($temp,0,50); } else { $final = substr($temp,0,$lastSpace); } Good luck! [/B]
Originally posted by jcarver I would say do something like this:
Good luck! [/B]
thanks if($lastSpace===false)
what is "===" meants? or typo?
It's pretty important actually (although not so much in this case).
It helps distinguish between false and 0, because PHP is so type-loose.
Read more about it here: http://www.php.net/strpos
~Carver