栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

截断包含HTML的文本,忽略标签

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

截断包含HTML的文本,忽略标签

假设您使用的是有效的XHTML,则解析HTML并确保正确处理标签非常简单。您只需要跟踪到目前为止已打开的标签,并确保“在您出门时”再次将其关闭。

<?phpheader('Content-type: text/plain; charset=utf-8');function printTruncated($maxLength, $html, $isUtf8=true){    $printedLength = 0;    $position = 0;    $tags = array();    // For UTF-8, we need to count multibyte sequences as one character.    $re = $isUtf8        ? '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;|[x80-xFF][x80-xBF]*}'        : '{</?([a-z]+)[^>]*>|&#?[a-zA-Z0-9]+;}';    while ($printedLength < $maxLength && preg_match($re, $html, $match, PREG_OFFSET_CAPTURE, $position))    {        list($tag, $tagPosition) = $match[0];        // Print text leading up to the tag.        $str = substr($html, $position, $tagPosition - $position);        if ($printedLength + strlen($str) > $maxLength)        { print(substr($str, 0, $maxLength - $printedLength)); $printedLength = $maxLength; break;        }        print($str);        $printedLength += strlen($str);        if ($printedLength >= $maxLength) break;        if ($tag[0] == '&' || ord($tag) >= 0x80)        { // Pass the entity or UTF-8 multibyte sequence through unchanged. print($tag); $printedLength++;        }        else        { // Handle the tag. $tagName = $match[1][0]; if ($tag[1] == '/') {     // This is a closing tag.     $openingTag = array_pop($tags);     assert($openingTag == $tagName); // check that tags are properly nested.     print($tag); } else if ($tag[strlen($tag) - 2] == '/') {     // Self-closing tag.     print($tag); } else {     // Opening tag.     print($tag);     $tags[] = $tagName; }        }        // Continue after the tag.        $position = $tagPosition + strlen($tag);    }    // Print any remaining text.    if ($printedLength < $maxLength && $position < strlen($html))        print(substr($html, $position, $maxLength - $printedLength));    // Close any open tags.    while (!empty($tags))        printf('</%s>', array_pop($tags));}printTruncated(10, '<b>&lt;Hello&gt;</b> <img src="world.png" alt="" /> world!'); print("n");printTruncated(10, '<table><tr><td>Heck, </td><td>throw</td></tr><tr><td>in a</td><td>table</td></tr></table>'); print("n");printTruncated(10, "<em><b>Hello</b>&#20;wxC3xB8rld!</em>"); print("n");

编码说明
:上面的代码假定XHTML是UTF-8编码的。还支持ASCII兼容的单字节编码,只需将其

false
作为第三个参数传递即可。不支持其他多字节编码,尽管您可能会
mb_convert_encoding
在调用该函数之前先转换为UTF-8,然后在每个
print
语句中再次转换回去,从而获得支持。

(不过,您应该 始终 使用UTF-8。)

编辑 :更新为处理字符实体和UTF-8。修复了以下错误:如果该字符是一个字符实体,该函数将打印一个字符过多。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/465005.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号