Posted By

mdavison on 04/14/11


Tagged

excerpt


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

notturnale


Excerpt


 / Published in: PHP
 

Creates excerpt that breaks at end of word.

  1. function excerpt($string='', $maxChar=50, $uri='#') {
  2. $length = strlen($string);
  3. if ($length < $maxChar) {
  4. return $string;
  5. }
  6. $trimmedString = substr($string, 0, $maxChar);
  7. $choppedString = substr($trimmedString, 0, strrpos($trimmedString, strrchr($trimmedString, ' ')));
  8. $newString = $choppedString . ' <a href="' . $uri . '">more</a>';
  9. return $newString;
  10. }

Report this snippet  

You need to login to post a comment.