I have the following function:
function truncate($string, $limit, $break=".", $pad="...") {
if(strlen($string) <= $limit) return $string;
if(false !== ($breakpoint = strpos($string, $break, $limit))) {
if($breakpoint < strlen($string) - 1) {
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
If i have the following code:
$html = '<div style="bla: bla;">somet30ext</div> <div id="bla">MORE AND MORE TEXT</div>';
print truncate($html, 30);
It will cut off at somet..... In other words, we end with:
<div style="bla: bla;">somet
I.e. an unclosed div tag. How do I go about solving this?
Update:
I don't want to truncate only when I get to the . I want something that can automatically add divs. In this case, the output should be:
<div style="bla: bla;">somet</div>
I.e. It actually added the because it knew it was unclosed? Am I correct in assuming I must use something like html purifier for this?