使用 PHP substr() 和 strip_tags(),同时保留格式且不破坏 HTML
我有各种 HTML 字符串可以剪切到 100 个字符(被剥离的内容,而不是原始内容),而无需剥离标签,也不会破坏 HTML。
原始 HTML 字符串(288 个字符):
$content = "<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div over <div class='nestedDivClass'>there</div>
</div> and a lot of other nested <strong><em>texts</em> and tags in the air
<span>everywhere</span>, it's a HTML taggy kind of day.</strong></div>";
标准修剪: 修剪为 100 个字符和 HTML 中断,剥离的内容约为 40 个字符:
$content = substr($content, 0, 100)."..."; /* output:
<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div ove... */
剥离的 HTML: > 输出正确的字符数,但明显丢失格式:
$content = substr(strip_tags($content)), 0, 100)."..."; /* output:
With a span over here and a nested div over there and a lot of other nested
texts and tags in the ai... */
部分解决方案:使用 HTML Tidy 或净化器关闭标签输出干净的 HTML,但 100 个 HTML 字符未显示内容。
$content = substr($content, 0, 100)."...";
$tidy = new tidy; $tidy->parseString($content); $tidy->cleanRepair(); /* output:
<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div ove</div></div>... */
挑战:输出干净的 HTML 和 n 个字符(不包括 HTML 元素的字符数):
$content = cutHTML($content, 100); /* output:
<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div over <div class='nestedDivClass'>there</div>
</div> and a lot of other nested <strong><em>texts</em> and tags in the
ai</strong></div>...";
类似问题
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
不神奇,但有效。
使用示例:
Not amazing, but works.
Usage example:
我并没有声称发明了这个,但是有一个非常完整的
Text::truncate()
方法 可以完成您想要的操作:I'm not claiming to have invented this, but there is a very complete
Text::truncate()
method in CakePHP which does what you want:使用 PHP 的 DOMDocument 类规范化 HTML 片段:
这个问题类似于 之前的问题,我在这里复制并粘贴了一个解决方案。如果 HTML 是由用户提交的,您还需要过滤掉潜在的 Javascript 攻击媒介,例如
onmouseover="do_something_evil()"
或HTML Purifier 这样的工具旨在捕获和解决这些问题,并且比我所使用的任何代码都要全面得多。可以发帖。
Use PHP's DOMDocument class to normalize an HTML fragment:
This question is similar to an earlier question and I've copied and pasted one solution here. If the HTML is submitted by users you'll also need to filter out potential Javascript attack vectors like
onmouseover="do_something_evil()"
or<a href="javascript:more_evil();">...</a>
. Tools like HTML Purifier were designed to catch and solve these problems and are far more comprehensive than any code that I could post.使用 HTML 解析器 并在文本的 100 个字符后停止。
Use a HTML parser and stop after 100 characters of text.
我做了另一个函数来做到这一点,它支持 UTF-8:
查看演示。
我建议在函数开始处使用
html_entity_decode
,这样它会保留 UTF-8 字符:I made another function to do it, it supports UTF-8:
SEE DEMO.
I recommend use
html_entity_decode
at the start of function, so it preserves the UTF-8 characters:您应该使用 Tidy HTML。您剪断字符串,然后运行 Tidy 来关闭标签。
(应得积分的积分)
You should use Tidy HTML. You cut the string and then you run Tidy to close the tags.
(Credits where credits are due)
无论您在开始时陈述的 100 个计数问题如何,您都在挑战中指出以下内容:
strip_tags(字符数
在实际显示的文本中
HTML)
这是我的建议:
基本上,我一边分析一边计算每个字符。我确保不计算任何 HTML 标记中的任何字符。我还会在最后检查一下,以确保当我停下来时我没有在说一个词的中间。一旦我停下来,我就会回到第一个可用的空间或>作为停止点。
这也会计算返回码等。考虑到它们会占用空间,我没有删除它们。
Regardless of the 100 count issues you state at the beginning, you indicate in the challenge the following:
strip_tags (the number of characters
in the actual displayed text of the
HTML)
Here is my proposal:
Bascially, I parse through each character counting as I go. I make sure NOT to count any characters in any HTML tag. I also check at the end to make sure I am not in the middle of a word when I stop. Once I stop, I back track to the first available SPACE or > as a stopping point.
This will also count the return codes etc. Considering they will take space, I have not removed them.
这是我在我的一个项目中使用的一个函数。它基于 DOMDocument,可与 HTML5 配合使用,比我尝试过的其他解决方案快大约 2 倍(至少在我的机器上,使用
html_cut($text, $max_length)
的时间为 0.22 毫秒 vs 0.43 毫秒) 500 个文本节点字符字符串的最佳答案,限制为 400)。Here is a function I'm using in one of my projects. It's based on DOMDocument, works with HTML5 and is about 2x faster than other solutions I've tried (at least on my machine, 0.22 ms vs 0.43 ms using
html_cut($text, $max_length)
from the top answer on a 500 text-node-characters string with a limit of 400).这是我对刀具的尝试。也许你们能发现一些错误。我发现其他解析器的问题是它们没有正确关闭标签并且它们在单词中间切入(等等)
Here is my try at the cutter. Maybe you guys can catch some bugs. The problem, i found with the other parsers, is that they don't close tags properly and they cut in the middle of a word (blah)
尝试以下操作:
这会将 HTML (strip_tags) 和限制字符 (mb_strimwidth) 限制为 160 个字符
Try the following:
This will strip the HTML (strip_tags) amd limit characters (mb_strimwidth) to 160 characters
尝试这个功能
并且
try this function
and
我知道这已经很老了,但我最近制作了一个用于剪切 HTML 进行预览的小类: https:// /github.com/Simbiat/HTMLCut/
您为什么要使用它而不是其他建议?以下是我想到的一些事情(摘自自述文件):
Class使用DOM进行操作,但在某些地方也使用了Regex(主要用于剪切和修剪)。也许它对某些人有用。
I know this is quite old, but I've recently made a small class for cutting HTML for previews: https://github.com/Simbiat/HTMLCut/
Why would you want to use that instead of the other suggestions? Here are a few things that come to my mind (taken from readme):
Class operates with DOM, but also uses Regex in some places (mainly for cutting and trimming). Perhaps it can be of use to some.
html_cut 的多字节版本,工作正常。
multibyte version of html_cut,it`s working fine.