irwan

Truncate Text at Word Break

Nov 17th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.56 KB | None | 0 0
  1. // Please acknowledge use of this code by including this header.
  2. function myTruncate($string, $limit, $break=".", $pad="...") {
  3.     // return with no change if string is shorter than $limit
  4.     if(strlen($string) <= $limit)
  5.         return $string;
  6.  
  7.     // is $break present between $limit and the end of the string?
  8.     if(false !== ($breakpoint = strpos($string, $break, $limit))) {
  9.         if($breakpoint < strlen($string) - 1) {
  10.             $string = substr($string, 0, $breakpoint) . $pad;
  11.         }
  12.     }
  13.     return $string;
  14. }
  15. /***** Example ****/
  16. $short_string=myTruncate($long_string, 100, ' ');
  17.  
Advertisement
Add Comment
Please, Sign In to add comment