Posted By

rliverman on 03/15/12


Tagged

php truncate


Versions (?)

Truncate Text Returned by PHP


 / Published in: PHP
 

URL: http://www.the-art-of-web.com/php/truncate/

// quick function to cut back on the length of content

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

Report this snippet  

You need to login to post a comment.