Last active
November 11, 2016 01:53
-
-
Save henrytran9x/7f29b4448fd10555fa671693ad1a731d to your computer and use it in GitHub Desktop.
Truncate String Words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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