Posted By

JoshChris on 11/19/13


Tagged

php trim break truncate word wordwrap


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Priestd09


PHP: Truncate a string at a word break


 / Published in: PHP
 

Truncates the given string at the specified length.

  1. /**
  2.  * Truncates the given string at the specified length.
  3.  *
  4.  * @param string $str The input string.
  5.  * @param int $width The number of chars at which the string will be truncated.
  6.  * @return string
  7.  */
  8. function truncate($str, $width) {
  9. return current(explode("\n", wordwrap($str, $width, "...\n")));
  10. }

Report this snippet  

You need to login to post a comment.