我有各种 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 或 purifier 关闭标签会输出干净的 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>...";
类似的问题
- 如何在不破坏标签的情况下剪辑 HTML 片段
- 剪切 HTML 字符串而不破坏 HTML 标签