Here is my code, what im doing is pulling text from a database, displaying in a table and where the data exceeds lets say 450 characters i put this on the end
….[view more]
Now the code works fine but there is one exception, the information in the database has html in it, like paragraphs and bullet lists. That poses a problem, the whole idea of putting a limit is so it doesnt stretch the row down further than i want it to go, a line break for a bullet list or a paragraph seems to be counted as 0 or 1 charatcers but it takes up the space of a lot of characters so how can i manipulate this code so that linebreaks are accounted for.
My ideas are to count the whitespace between with something like this:
$white_space = substr_count($text, ' ');
Which returns the total whitespace
I also tried this
$white_space_str = substr_count($newstr, ' ');
But that returns 0 so im doing something wrong.
But in any case im a bit stuck at this point and hoping someone can help out a newbie, if the code is simplified rather than trimmed and neat it might help me understand it better 🙂
But im not sure how to put that into a working code.
function trim_description($str, $maxlen) {
if ( strlen($str) <= $maxlen ) return $str;
$newstr = substr($str, 0, $maxlen);
if ( substr($newstr,-1,1) != ' ' ) $newstr = substr($newstr, 0, strrpos($newstr, " "));
return $newstr;
}
Maybe this can help you. I found this as an answer for this question