Posted By

lfatr on 02/16/10


Tagged


Versions (?)

TRIM A STRING WITHOUT CUTTING ANY WORDS


 / Published in: PHP
 

URL: http://www.justin-cook.com/wp/2006/06/27/php-trim-a-string-without-cutting-any-words/

  1. function neat_trim($str, $n, $delim='…') {
  2. $len = strlen($str);
  3. if ($len > $n) {
  4. preg_match('/(.{' . $n . '}.*?)\b/', $str, $matches);
  5. return rtrim($matches[1]) . $delim;
  6. }
  7. else {
  8. return $str;
  9. }
  10. }

Report this snippet  

You need to login to post a comment.