Skip to content

Instantly share code, notes, and snippets.

@lucasreal
Created August 1, 2012 19:48
Show Gist options
  • Save lucasreal/3230072 to your computer and use it in GitHub Desktop.
Save lucasreal/3230072 to your computer and use it in GitHub Desktop.
Cut the given string but not in the middle of the word.
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