Reputation: 3786
if (strlen($body) > 2000)
{
$body = substr($body, 0, 2000);
$body .= '...<a href="/blogpost/' . $id . '/' . urlencode($title) .'">Read More</a>
';
}
So say i have <font size="5">
when it cuts it off, its like "And he was like<font...Read More
". It shows the word <font
on the screen for the end user to read, because it didn't get a chance to close because it was truncated. Is there a fix or work around for this? Its kinda annoying to be like this.
Upvotes: 0
Views: 303
Reputation: 15451
When truncating HTML for abbreviated views, I recommend throwing a strip_tags
first, generally you don't really need formatting/links on abbreviated content, imo.
$plainBody=strip_tags($body);
$abrvBody=strlen($plainBody)>2000?susbtr($plainBody,0,2000):$plainBody;
If you must keep your formatting/links, you can always clean up the chopped text with preg_replace
$cleanAbrvBody=preg_replace('/<[^>]+$/','',susbtr($body,0,2000));
Though you may end up with un-closed tags.
Upvotes: 1