Php 截断文本而不截断HTML
此字符串包含78个带HTML的字符,39个不带HTML的字符:Php 截断文本而不截断HTML,php,Php,此字符串包含78个带HTML的字符,39个不带HTML的字符: <p>I really like the <a href="http://google.com">Google</a> search engine.</p> 我真的很喜欢这个搜索引擎 我希望根据非HTML字符计数截断此字符串,因此,例如,如果我希望将上述字符串截断为24个字符,则输出将为: I really like the <a href="http://google.co
<p>I really like the <a href="http://google.com">Google</a> search engine.</p>
我真的很喜欢这个搜索引擎
我希望根据非HTML字符计数截断此字符串,因此,例如,如果我希望将上述字符串截断为24个字符,则输出将为:
I really like the <a href="http://google.com">Google</a>
我真的很喜欢
在确定要截断的字符数时,截断没有考虑html,它只考虑剥离计数。但是,它没有留下打开的HTML标记。好吧,这就是我放在一起的东西,它似乎在工作:
function truncate_html($string, $length, $postfix = '…', $isHtml = true) {
$string = trim($string);
$postfix = (strlen(strip_tags($string)) > $length) ? $postfix : '';
$i = 0;
$tags = []; // change to array() if php version < 5.4
if($isHtml) {
preg_match_all('/<[^>]+>([^<]*)/', $string, $tagMatches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
foreach($tagMatches as $tagMatch) {
if ($tagMatch[0][1] - $i >= $length) {
break;
}
$tag = substr(strtok($tagMatch[0][0], " \t\n\r\0\x0B>"), 1);
if ($tag[0] != '/') {
$tags[] = $tag;
}
elseif (end($tags) == substr($tag, 1)) {
array_pop($tags);
}
$i += $tagMatch[1][1] - $tagMatch[0][1];
}
}
return substr($string, 0, $length = min(strlen($string), $length + $i)) . (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '') . $postfix;
}
函数truncate_html($string,$length,$postfix='&hellip;',$isHtml=true){
$string=修剪($string);
$postfix=(strlen(strip_标记($string))>$length)?$postfix:“”;
$i=0;
$tags=[];//如果php版本<5.4,则更改为array()
如果($isHtml){
preg_match_all('/]+>([^我建议您研究一下XML解析器;它们可能是确保您不破坏HTML/了解显示的文本是什么或不是什么的唯一方法。单字母变量,不解释参数是什么。希望在这里看到一些解释,因为此函数看起来非常漂亮和简洁。这添加了“后缀”在最后一个结束标记之后。这在某些情况下可能是可取的,但在我的情况下,它会导致“…”在最后一行单独结束。不太美观。
truncate_html('<p>I really like the <a href="http://google.com">Google</a> search engine.</p>', 24);