Take a look at the logic of the function:
It splits the string by a space, counts and slices the resulting array and puts them back together.
Now a space is a delimiter for words... on what char do we need to split the string to get all characters instead of words? Right, on nothing (better to say: an empty string)!
So you change both of these lines
function word_trim($string, $count, $ellipsis = FALSE){
$words = explode(' ', $string);
if (count($words) > $count){
//...
$string = implode(' ', $words);
}
//...
}
to
$words = str_split($string);
//...
$string = implode('', $words);
and you should be fine.
Note that i changed the first explode
-call to str_split
, as explode
doesn't accept an empty delimiter (according to the manual).
I'd rename the function to character_trim
or something else and perhaps the $word
variable, too, so your code will make sense to a reader.