Forked from luizventurote/truncate-string-words.php
Created
February 1, 2016 10:17
-
-
Save henrytran9x/79cc6b4819d02f807303 to your computer and use it in GitHub Desktop.
Truncate a string provided by the maximum limit without breaking a word.
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 | |
/** | |
* 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