June 27th, 2006
PHP: Trim a String Without Cutting Any Words
I previously posted a function that allows you to trim a string in ASP without chopping any words in half. It works by trimming to the space nearest the desired string length. Here's the duplicate function in PHP.
It's called neat_trim.
/**
* Cut string to n symbols and add delim but do not break words.
*
* Example:
* <code>
* $string = 'this sentence is way too long';
* echo neat_trim($string, 16);
* </code>
*
* Output: 'this sentence is…'
*
* @access public
* @param string string we are operating with
* @param integer character count to cut to
* @param string|NULL delimiter. Default: '…'
* @return string processed string
**/
function neat_trim($str, $n, $delim='…') {
$len = strlen($str);
if ($len > $n) {
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
return rtrim($matches[1]) . $delim;
}
else {
return $str;
}
}
* Cut string to n symbols and add delim but do not break words.
*
* Example:
* <code>
* $string = 'this sentence is way too long';
* echo neat_trim($string, 16);
* </code>
*
* Output: 'this sentence is…'
*
* @access public
* @param string string we are operating with
* @param integer character count to cut to
* @param string|NULL delimiter. Default: '…'
* @return string processed string
**/
function neat_trim($str, $n, $delim='…') {
$len = strlen($str);
if ($len > $n) {
preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
return rtrim($matches[1]) . $delim;
}
else {
return $str;
}
}

on April 4th, 2007 at 5:16 am
Very handy function that, I was asked to add a feature to the company news listings on our homepage that would only display the first 10 words of a new post, this did the trick with just one change:
This makes the regex search for any whitespace and returns a match each time.
on June 22nd, 2007 at 5:50 pm
this works great. thanks!
on August 8th, 2007 at 10:25 am
johnny, i second that. one year after it's posted, it still helps a clueless dude
on August 21st, 2007 at 8:56 am
And I second had3z! A year later, but still entirely useful.
on October 21st, 2007 at 4:47 am
this worked great for me, but when i wanted to trim more than 100 it did not work. Very new to php so can anyone help
on October 24th, 2007 at 4:28 pm
Worked fine with 200 characters, thanks!
on December 7th, 2007 at 1:20 pm
Script works great! A friend told me that the programmers behind the Earthoid Search Engine use your script! Right On!
on January 8th, 2008 at 4:15 pm
It's good, but the usage of regular expression here is superfluous. A simple substr function could better the script in so many ways
Nice way to get the topic discussion going though.
on April 17th, 2008 at 2:38 pm
Hi justin
Thanks, this is a great script!
I noticed that if the nearest space to the cut-off point is at the end of the string, then the '…' is still added at the end, even though the string hasn't been trimmed.
I came up with this solution. It's probably not very good code, but it worked for me:
if(str_replace($delim,",$trStr) == $str) $trStr = $str;
return $trStr;
replaces:
Just thought it might be useful to share