Last active
April 8, 2016 08:49
-
-
Save luizventurote/10309332 to your computer and use it in GitHub Desktop.
Truncate a string provided by the maximum limit without breaking a word.
This file contains 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 | |
/** | |
* truncate a string provided by the maximum limit without breaking a word | |
* @param string $str | |
* @param integer $maxlen | |
* @return string | |
*/ | |
function truncateStringWords($str, $maxlen) { | |
if (strlen($str) <= $maxlen) return $str; | |
$newstr = substr($str, 0, $maxlen); | |
if (substr($newstr, -1, 1) != ' ') $newstr = substr($newstr, 0, strrpos($newstr, " ")); | |
return $newstr.' ...'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use it a little changed