OK, I suppose I need to clarify what I need.
I have long line of text. Say I need to limit it no more then 50 chars. I want it to get to my limit and find a space between words to cut it off and put .... Now, it's fine with me if there is a small deviation. Let's say I have a break between word after char 53. And the word preceding that break is really long. Instead of drastically shrtening this line it is OK to put ... after char53, but i'm afraid this is a whole new layer of code.
I've tried both codes:
function limit_char($str, $len) {
if (strlen($str) > $len)
return substr($str, 0, strpos($str," ",$len)) . "...";
else
return $str;
}
Worked well, but on one line abbreaviated it entirely to ....
function limit_char($str, $len) {
if (strlen($str) > $len)
{ //you removed
$tmp = substr($str,$len,1);
if (substr($tmp,-1) == ' ')
return $tmp . "...";
else //edited to clean up uneeded brackets
for($i=1;$i<$len,$i++; ) //may need to start at 0 but I'm thinking 1 is right)
if (substr($tmp,-1*$i,1) == ' ')
return substr($tmp,0,$len-$i);
} //i forgot
else
return $str;
}
For some reason hangs my entire page and then loads the following error:
Fatal error: Maximum execution time of 30 seconds exceeded in page.php on line 39
Line 39 is:
if (substr($tmp,-1*$i,1) == ' ')