剥离标签以及介于两者之间的所有内容 [英] Strip Tags and everything in between

查看:67
本文介绍了剥离标签以及介于两者之间的所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何剥离<h1>including this content</h1>

我知道您可以使用剥离标签删除标签,但我希望两者之间的所有内容都消失了.

I know you can use strip tags to remove the tags, but i want everything in between gone as well.

任何帮助将不胜感激.

Any help would be appreciated.

推荐答案

在处理HTML时,应使用HTML解析器对其进行正确处理.您可以使用PHP的 DOMDocument 并使用 DOMXPath ,例如:

As you’re dealing with HTML, you should use an HTML parser to process it correctly. You can use PHP’s DOMDocument and query the elements with DOMXPath, e.g.:

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
foreach ($xpath->query('//h1') as $node) {
    $node->parentNode->removeChild($node);
}
$html = $doc->saveHTML();

这篇关于剥离标签以及介于两者之间的所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆