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;
   }
}

9 Responses to ' PHP: Trim a String Without Cutting Any Words '

Subscribe to comments with RSS or TrackBack to ' PHP: Trim a String Without Cutting Any Words '.

  1. Andy Gibson said,

    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:

    preg_match('/(.{' . $n . '}.*\s)\b/', $str, $matches);

    This makes the regex search for any whitespace and returns a match each time.

  2. johnny said,

    on June 22nd, 2007 at 5:50 pm

    this works great. thanks!

  3. had3z said,

    on August 8th, 2007 at 10:25 am

    johnny, i second that. one year after it's posted, it still helps a clueless dude :)

  4. Graham said,

    on August 21st, 2007 at 8:56 am

    And I second had3z! A year later, but still entirely useful.

  5. daz said,

    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

  6. Andrea said,

    on October 24th, 2007 at 4:28 pm

    Worked fine with 200 characters, thanks!

  7. Diego said,

    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!

  8. Anon said,

    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 :P Nice way to get the topic discussion going though.

  9. Rob said,

    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:

    $trStr = rtrim($matches[1]) . $delim;
    if(str_replace($delim,",$trStr) == $str) $trStr = $str;
    return $trStr;

    replaces:

    return rtrim($matches[1]) . $delim;

    Just thought it might be useful to share :)

Leave a reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-Spam Image