BLOG

A LITTLE STORY ABOUT MY JOURNEY

Making sure PHP substr finishes on a word not a character

t could be done with a regex, something like this will get up to 260 characters from the start of string up to a word boundary:

$line=$body;
if (preg_match('/^.{1,260}\b/s', $body, $match))
{
$line=$match[0];
}

OR

$s = substr($string, 0, 261);
$result = substr($s, 0, strrpos($s, ' '));

Rerence: http://stackoverflow.com/questions/1233290/making-sure-php-substr-finishes-on-a-word-not-a-character

344 Total Views 1 Views Today