Php Tidy:删除身体内的链接和样式标签 [英] Php Tidy : remove link and style tags inside body

查看:143
本文介绍了Php Tidy:删除身体内的链接和样式标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须清理一些HTML代码,以移除< style> < link> code>< body> 标记。
我已经使用PHP Tidy做了一些清理,但是我没有找到如何使用PHP Tidy去除这些标记。

I must cleanup some HTML code to remove <style> and <link> tags inside the <body> tag. I'm already using PHP Tidy to do some cleanup but I did not found how to remove those tags with PHP Tidy.

你有解决方案吗?或者,也许另一个标记清理PHP类...

Do you have a solution ? Or maybe another markup cleaner PHP class...

推荐答案

不知道如何用Tidy做到这一点,但可以使用DOM

Don't know how to do that with Tidy, but you can use DOM

$dom = new DOMDocument;                    // init new DOMDocument
$dom->loadHTML($html);                     // load HTML into it
$xpath = new DOMXPath($dom);               // create a new XPath
$nodes = $xpath->query('//body/style');    // Find all style elements in body tag
foreach($nodes as $node) {                 // Iterate over found elements
    $node->parentNode->removeChild($node); // Remove complete style node
}
echo $dom->saveHTML();                     // output cleaned HTML

对于< link> 元素,将Xpath调整为 // body / link

For the <link> elements, adjust the Xpath to //body/link.

这篇关于Php Tidy:删除身体内的链接和样式标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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