使用PHP substr()和strip_tags(),同时保留格式并且不会破坏HTML [英] Using PHP substr() and strip_tags() while retaining formatting and without breaking HTML
问题描述
我有各种不同的HTML字符串可以分割为100个字符(剥离的内容,不是原始的),不会剥离标签,也不会破坏HTML。
原始HTML字符串<288个字符):
$ content =< div>使用< span class = 'spanClass'> span over here< / span>和一个
< div div =" divClass>嵌套div< div class ='nestedDivClass'> there< / div>
< / div>和其他嵌套的< strong>< em>文本< / em>以及空中标签
< span>无处不在< / span>,这是一种HTML标签类型的一天< /强>< / DIV>中;
标准修剪:修剪为100个字符和HTML中断, 〜40个字符:
$ content = substr($ content,0,100)。...; / *输出:
< div>使用&span; span&span> spanClass>跨越此处< / span>和
< div class ='divClass'>嵌套div ove ... * /
剥离的HTML:输出正确的字符数,但显然不符合格式:
$ content = substr (strip_tags($ content)),0,100)。...; / *输出:
使用HTML Tidy或purifier关闭标签可以输出纯净的HTML,但是100个字符的HTML不会显示内容。
在这里有一个跨度和一个嵌套的div,以及其他许多嵌套的
文本和标签在ai ... * /
$ content = substr($ content,0,100)。...;
$ tidy = new tidy; $ tidy-> parseString($内容); $ tidy-> cleanRepair(); / *输出:
< div>使用&span; span&span> spanClass>跨越此处< / span>和
< div class ='divClass>嵌套div ove< / div>< / div> ... * /
挑战:要输出干净的HTML和 n 字符(不包括HTML元素的字符数):$ content = cutHTML($ content,100); / *输出:
< div>使用&span; span&span> spanClass>跨越此处< / span>和
< div class ='divClass'>嵌套div< div class ='nestedDivClass'> there< / div>
< / div>和许多其他嵌套的< strong>< em>文本< / em>和
ai< / strong>< / div> ...;
相似的问题
- 剪切HTML字符串而不破坏HTML标签
函数html_cut($ text,$ max_length)解决方案
)
{
$ tags = array();
$ result =;
$ is_open = false;
$ grab_open = false;
$ is_close = false;
$ in_double_quotes = false;
$ in_single_quotes = false;
$ tag =;
$ i = 0;
$ stripped = 0;
$ stripped_text = strip_tags($ text);
while($ i< strlen($ te xt)&& $剥离< strlen($ stripped_text)&& $剥离< $ max_length)
{
$ symbol = $ text {$ i};
$ result。= $ symbol;
switch($ symbol)
{
case'<':
$ is_open = true;
$ grab_open = true;
休息; ($ in_double_quotes)
$ in_double_quotes = false;
else
$ in_double_quotes = true;
break;
case':
if($ in_single_quotes)
$ in_single_quotes = false;
else
$ in_single_quotes = true;
break;
case'/':
if($ is_open&!$ in_double_quotes&&!$ in_single_quotes)
{
$ is_close = true;
$ is_open = false;
$ grab_open = false;
}
break;
$ b $ ('$ is_open)
$ grab_open = false;
else
$剥离++;
break;
$ b $ case'>':
if($ is_open)
{
$ is_open = false;
$ grab_open = false;
array_push($ tags,$ tag);
$ tag =;
}
else if($ is_close)
{
$ is_close = false;
array_pop($ tags);
$ tag =;
}
break;
默认值:
if($ grab_open || $ is_close)
$ tag。= $ symbol;
如果(!$ is_open&&!$ is_close)
$剥离++;
}
$ i ++;
}
while($ tags)
$ result。=< /。array_pop($ tags)。>;
返回$ result;
$ / code>
用法示例:
$ content = html_cut($ content,100);
I have various HTML strings to cut to 100 characters (of the stripped content, not the original) without stripping tags and without breaking HTML.
Original HTML string (288 characters):
$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>";
Standard trim: Trim to 100 characters and HTML breaks, stripped content comes to ~40 characters:
$content = substr($content, 0, 100)."..."; /* output:
<div>With a <span class='spanClass'>span over here</span> and a
<div class='divClass'>nested div ove... */
Stripped HTML: Outputs correct character count but obviously looses formatting:
$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... */
Partial solution: using HTML Tidy or purifier to close off tags outputs clean HTML but 100 characters of HTML not displayed content.
$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>... */
Challenge: To output clean HTML and n characters (excluding character count of HTML elements):
$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>...";
Similar Questions
Not amazing, but works.
function html_cut($text, $max_length)
{
$tags = array();
$result = "";
$is_open = false;
$grab_open = false;
$is_close = false;
$in_double_quotes = false;
$in_single_quotes = false;
$tag = "";
$i = 0;
$stripped = 0;
$stripped_text = strip_tags($text);
while ($i < strlen($text) && $stripped < strlen($stripped_text) && $stripped < $max_length)
{
$symbol = $text{$i};
$result .= $symbol;
switch ($symbol)
{
case '<':
$is_open = true;
$grab_open = true;
break;
case '"':
if ($in_double_quotes)
$in_double_quotes = false;
else
$in_double_quotes = true;
break;
case "'":
if ($in_single_quotes)
$in_single_quotes = false;
else
$in_single_quotes = true;
break;
case '/':
if ($is_open && !$in_double_quotes && !$in_single_quotes)
{
$is_close = true;
$is_open = false;
$grab_open = false;
}
break;
case ' ':
if ($is_open)
$grab_open = false;
else
$stripped++;
break;
case '>':
if ($is_open)
{
$is_open = false;
$grab_open = false;
array_push($tags, $tag);
$tag = "";
}
else if ($is_close)
{
$is_close = false;
array_pop($tags);
$tag = "";
}
break;
default:
if ($grab_open || $is_close)
$tag .= $symbol;
if (!$is_open && !$is_close)
$stripped++;
}
$i++;
}
while ($tags)
$result .= "</".array_pop($tags).">";
return $result;
}
Usage example:
$content = html_cut($content, 100);
这篇关于使用PHP substr()和strip_tags(),同时保留格式并且不会破坏HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!