PHP 提取HTML中的文本内容
PHP 提取HTML中的文本内容并剔除制表符
PHP对HTML的操作还是很方便的。
只需用到PHP中的 三个方法 strip_tags、 html_entity_decode、 preg_replace 就可以轻松搞定了。
代码如下:
public function getRichBrief(string $html): string
{
$content = strip_tags($html); // 去除html标签
$content = html_entity_decode($content); // 将 `html转义字符` 转换为 `普通字符`
return preg_replace("/(\s|\ \;| |\xc2\xa0)/", "", $content); // 剔除制表符、空
}
参考网址:
html_entity_decode
strip_tags
preg_replace
正则表达式全集
本作品采用《CC 协议》,转载必须注明作者和本文链接
学到了
朋友你听过xpath嘛 :grin: