Truncate Text Returned by PHP


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  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. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.