I have a code to trim the long word for the specific length and replace it with '...' but, it has a problem, where, if user's name dont have space, its will look wierd..
heres the code:
<?
function truncateString($string, $length, $append = '...') {
$length -= strlen($append); // length of "..."
$string = substr($string, 0, $length);
$string = substr($string, 0, strrpos($string, ' '));
$string .= $append;
return $string;
}
//to test the function
echo truncateString('My very long title', 15);
?>
1. if value has space,it will look nice
exp: John Mayer ...
2. if dont have space
exp: ...
How to fix it?