PHP简单的html DOM从html标签中移除所有属性 [英] PHP simple html DOM remove all attributes from an html tag
问题描述
$html = file_get_html('page.php');
foreach($html->find('p') as $tag_name)
{
$attr = substr($tag_name->outertext,2,strpos($tag_name->outertext, ">")-2);
$tag_name->outertext = str_replace($attr, "", $tag_name->outertext);
}
echo $html->innertext;
以上是我编写的代码,用于介绍< p>
标记在我的html页面并将它们删除。
我的html代码与此类似:
Above is the code I wrote to take what's inside all <p>
tags in my html page and remove them.
My html code is similar to this :
<p class="..." id = "..." style = "...">some text...</p>
<p class="..." id = "..." style = "...">some text...</p>
<p class="..." id = "..." style = "...">some text...</p>
<font>
<p class="..." id = "..." style = "...">some text ...</p>
<p class="..." id = "..." style = "...">some text ...</p>
</font>
<p class="..." id = "..." style = "...">some text...</p>
如果我运行php代码,结果如下所示:
If I run the php code , result would be this :
<p>some text...</p>
<p>some text...</p>
<p>some text...</p>
<font>
<p class="..." id = "..." style = "...">some text ...</p>
<p class="..." id = "..." style = "...">some text ...</p>
</font>
<p>some text...</p>
不会删除< p>
标记属性位于< font>
。
如果有人可以帮助我,我会很感激。
It doesn't remove <p>
tags attributes that are inside <font>
.
If anyone can help me with this I'll be appreciate.
推荐答案
当我使用您的代码和示例HTML时,它不会从所有 < p>
标签,甚至是< font>
中的标签,所以我不确定你为什么不工作。
When I use your code and example HTML, it does remove all the attributes from all the <p>
tags, even the ones inside <font>
, so I'm not sure why yours isn't working.
但看起来像 simplehtmldom有方法专门处理属性,所以你不必使用字符串函数:
But it looks like simplehtmldom has methods that specifically deal with attributes so you don't have to use string functions:
$html = file_get_html('page.php');
foreach($html->find('p') as $p) {
foreach ($p->getAllAttributes() as $attr => $val) {
$p->removeAttribute($attr);
}
}
echo $html->innertext;
希望这会更有效。
这篇关于PHP简单的html DOM从html标签中移除所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!