使用 php 按元素 id 清理 html 字符串
正如您从主题中看到的,我正在寻找一个使用 HTML id 属性清理 php 中的 HTML 字符串的工具,例如:
根据以下 PHP 字符串,我希望清理 HTML,删除 black11
$test = '
<div id="block1">
<div id="block11">Hello1 <span>more html here...</span></div>
<div id="block12">Hello2 <span>more html here...</span></div>
</div>
<div id="block2"></div>
';
。
$test = '
<div id="block1">
<div id="block12">Hello2 <span>more html here...</span></div>
</div>
<div id="block2"></div>
';
我已经尝试过 htmlpurifier.org 的工具,但无法获得理想的结果 我唯一实现的就是通过标签删除元素;删除ID;擦除类。
有没有简单的方法可以使用净化器或其他来实现这一目标?
提前致谢,
as you can see by the subject am looking for a tool for cleaning up a HTML string in php using a HTML id property, example:
According to the following PHP string I wish to clean the HTML erasing the black11
$test = '
<div id="block1">
<div id="block11">Hello1 <span>more html here...</span></div>
<div id="block12">Hello2 <span>more html here...</span></div>
</div>
<div id="block2"></div>
';
Will became
$test = '
<div id="block1">
<div id="block12">Hello2 <span>more html here...</span></div>
</div>
<div id="block2"></div>
';
I already tried the tool from htmlpurifier.org and can't get the desirable result. Only thing I achieved was removing elements by tag; erasing id; erasing class.
Is there any simple way to achieve this using purifier or other?
Thanks in advance,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
作为操作 HTML 数据的通用解决方案,我建议:
DOMDocument::loadHTML
DOMDocument::getElementById
DOMNode::removeChild
DOMDocument::saveHTML
Note : it'll add some tags arround your HTML, as `DOMDocument::saveHTML` generates the HTML that corresponds to a full HTML Document :-(
几个
str_replace
来删除这些可能没问题,我想...这不是工作中最困难的部分,并且应该可以正常工作。As a general solution for manipulating HTML data, I would recommend :
DOMDocument::loadHTML
DOMDocument::getElementById
DOMNode::removeChild
DOMDocument::saveHTML
Note : it'll add some tags arround your HTML, as `DOMDocument::saveHTML` generates the HTML that corresponds to a full HTML Document :-(
A couple of
str_replace
to remove those might be OK, I suppose... It's not the hardest part of the work, and should work fine.