Php Tidy:删除正文中的链接和样式标签

发布于 2024-09-05 12:31:06 字数 198 浏览 7 评论 0原文

我必须清理一些 HTML 代码以删除 标记内的

你有解决办法吗?或者也许是另一个标记清理 PHP 类......

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

预谋 2024-09-12 12:31:06

不知道如何使用 Tidy 做到这一点,但您可以使用 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

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

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

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

疯狂的代价 2024-09-12 12:31:06

Tidy 的替代方案是 http://htmlpurifier.org/

HTML Purifier 是一个符合标准的
HTML 过滤器库编写
PHP。 HTML Purifier 不仅会删除所有恶意
代码(更广泛地称为 XSS)经过彻底审核,
安全但宽松的白名单,
它还将确保您的文件
符合标准,只有通过
对 W3C 规范的全面了解。


将此作为一个附加答案,因为它与 DOM 解决方案完全无关。

An alternative to Tidy would be http://htmlpurifier.org/

HTML Purifier is a standards-compliant
HTML filter library written in
PHP. HTML Purifier will not only remove all malicious
code (better known as XSS) with a thoroughly audited,
secure yet permissive whitelist,
it will also make sure your documents are
standards compliant, something only achievable with a
comprehensive knowledge of W3C's specifications.


Made this an additional answer, since it is so completely unrelated to the DOM solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文