Skip to content

Instantly share code, notes, and snippets.

@henrytran9x
Last active November 11, 2016 01:53
Show Gist options
  • Save henrytran9x/7f29b4448fd10555fa671693ad1a731d to your computer and use it in GitHub Desktop.
Save henrytran9x/7f29b4448fd10555fa671693ad1a731d to your computer and use it in GitHub Desktop.
Truncate String Words
<?php
function truncateStringWords($str, $maxlen,$replace = '...') {
if (strlen($str) <= $maxlen) return $str;
$newstr = substr($str, 0, $maxlen);
if (substr($newstr, -1, 1) != ' ') $newstr = substr($newstr, 0, strrpos($newstr, " "));
return $newstr.$replace;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment