How to truncate HTML to certain number of characters?

限于喜欢 提交于 2019-12-01 18:53:21

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!