问题描述
此字符串包含HTML的78个字符和不包含HTML的39个字符:
This string has 78 characters with HTML and 39 characters without HTML:
<p>I really like the <a href="http://google.com">Google</a> search engine.</p>
我想基于非HTML字符数截断该字符串,因此例如,如果我想将上述字符串截断为24个字符,则输出为:
I want to truncate this string based on the non-HTML character count, so for example if I wanted to truncate the above string to 24 characters, the output would be:
I really like the <a href="http://google.com">Google</a>
在确定要截断的字符数时,截断未考虑html,它仅考虑了剥离计数.但是,它并没有留下开放的HTML标记.
The truncation did not take into account the html when determining the number of characters to cut off, it only considered the stripped count. However, it didn't leave open HTML tags.
推荐答案
好的,这就是我整理的内容,似乎可以正常工作:
Alright so this is what I put together and it seems to be working:
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('<p>I really like the <a href="http://google.com">Google</a> search engine.</p>', 24);
该功能是从(经过小的修改)中获取的:
The function was grabbed from (made a small modification):
http://www.dzone.com/snippets/truncate-text- preserving-html
这篇关于截断文本而不截断HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!