Created
August 1, 2012 19:48
-
-
Save lucasreal/3230072 to your computer and use it in GitHub Desktop.
Cut the given string but not in the middle of the 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
function addEllipsis($text, $maxChars, $splitter = '...'){ // you could put a splitter | |
$x = $maxChars; | |
if($maxChars < strlen($text)){ //check if the string have more characters than the limit. | |
while($text[$x]!= ' ' && $x > 0 ){ //pass thru the string looking for a space. | |
$x--; | |
} | |
$theReturn = substr($text,0,$x).$splitter; //get the slice of the string and concat the splitter. | |
}else{ //if the string have no more than the characteres limit | |
$theReturn = $text; | |
} | |
$theReturn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment