Posted By

level09 on 08/18/09


Tagged


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

luman
umang_nine
ReeceMarsland


Truncate text at word break


 / Published in: PHP
 

  1. function myTruncate($string, $limit, $break=".", $pad="...") {
  2. // return with no change if string is shorter than $limit
  3. if(strlen($string) <= $limit)
  4. return $string;
  5.  
  6. // is $break present between $limit and the end of the string?
  7. if(false !== ($breakpoint = strpos($string, $break, $limit))) {
  8. if($breakpoint < strlen($string) - 1) {
  9. $string = substr($string, 0, $breakpoint) . $pad;
  10. }
  11. }
  12. return $string;
  13. }
  14. /***** Example ****/
  15. $short_string=myTruncate($long_string, 100, ' ');

Report this snippet  

You need to login to post a comment.