I have some content (some from an external source, some specially written) that is displayed in a kind of blog format. I want to cut off the text after a certain number of characters, so currently I'm doing this:
<?=substr( strip_tags($a['content']), 0, 400 )?>
The problem is, this loses all the formatting and I just get one massive blob of text. But if I don't strip the tags, obviously some tags will go unclosed and mess up the layout.
What would be a good way to truncate after X number of characters, but also keep some basic formatting?
Not convinced if this is the best approach for your problem, but it is a simple one, and would work if you're only concerned about preserving 1 or 2 tags to retain some basic formatting in the html.
So, you could do something like: find the tags you want to keep and replace them with some unique combination of characters, then after truncating the string, find/replace the unique string combination you created with the tag you originally replaced them with.
$content = str_replace("<br/>", "\n", $a['content']);
$content = substr(strip_tags($content), 0, 400);
echo str_replace("\n", "<br/>", $content);
http://snippets.dzone.com/posts/show/7125
That will auto-close any tags that got snipped. The versions in the comments seem to be better.
This might not answer your question but this is one method to solve this issue.
If I was writing a blog I'd want to define where the post would be truncated. This way I'd be able to post stuff like a video and truncate the post at where the video ends and then the rest can be displayed when the post is viewed.
Try adding a specific string at where you want the post to be truncated e.g. "--" or some tag so the break location isn't visable.
Then from there you can use mb_stripos to store the location of the break string and then pass that location as the length for the substring method.
or...
<?=substr( strip_tags($a['content']), 0, mb_stripo(strip_tags($a['content']),
"break string"))?>
This might not be as accurate as you want, but if you could be sure that only text markups such as headings links and paragraphs where being used, something like:
$i = 0;
while($i < 400){
$i = strpos($string, '</p>', $i) + 4;
}
echo substr( $string, 0, strpos($string, '</p>', $i)+4);
It would mean you would have variable string lengths but they would be as close as possible to the nearest paragraph.
来源:https://stackoverflow.com/questions/2974833/how-to-truncate-html-to-certain-number-of-characters