Skip to content

Instantly share code, notes, and snippets.

@patrick-mota
Created October 3, 2017 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrick-mota/cd402e371a0f839717ad73de4cee6d43 to your computer and use it in GitHub Desktop.
Save patrick-mota/cd402e371a0f839717ad73de4cee6d43 to your computer and use it in GitHub Desktop.
truncate words php
public function truncate($string, $length=100, $append="…") {
$string = trim($string);
if(strlen($string) > $length) {
$string = wordwrap($string, $length);
$string = explode("\n", $string, 2);
$string = $string[0] . $append;
}
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment