Say I have the following loop in my view
foreach ($value as $row):
echo $row['name'] . ', ';
endforeach;
This outputs a string like this in my browser
Geddy, Lee, Neil, Peart, Alex,
I wonder if anyone can suggest a method to truncate this string at n characters, for example
Geddy, Lee, Ne…
Since the string is being output from the loop I am unsure how to wrap a truncate function around this foreach.
Thanks for helping!
sample truncate function
function truncate_text($text, $nbrChar, $append='...') {
if(strlen($text) > $nbrChar) {
$text = substr($text, 0, $nbrChar);
$text .= $append;
}
return $text;
}
Why not save the row values to a variable and truncate that variable and just echo that?