DOM php按标签名称删除所有标签 [英] DOM php delete all tags by tag name
本文介绍了DOM php按标签名称删除所有标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个脚本问题我一直在使用一段时间,它为我工作,直到我遇到这个问题。
我有一个脚本我希望从html源代码中删除所有的p html标签。
该脚本的工作部分原因是它只删除了一些p标签,但留下了一些。
我不明白为什么这样做。 / p>
$ doc = new DOMDocument();
$ a =<< FAIL
< html>< body>
< div style =clear:both>< / div>
< p class =articletitle> hoo< / p>
< p class =articletext> hmmm< / p>
< p class =articletext> hmmmm< / p>
< p align =center>< / p>
< / body>< / html>
失败;
$ doc-> loadHTML($ a);
$ list = $ doc-> getElementsByTagName(p);
foreach($ list as $ l){
$ 1-> parentNode-> removeChild($ l);
$ c ++;
}
echo $ doc-> saveHTML()。 $ C;
脚本返回
<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.0 Transitional // ENhttp://www.w3.org/TR/REC-html40/loose.dtd\">
< html>< body>
< div style =clear:both>< / div>
< p class =articletext> hmmm< / p>
< p align =center>< / p>
省略两个p标签...
<你可以帮我找出为什么要跳过一些标签
解决方案
尝试这样:
$ doc-> loadHTML($ a);
$ list = $ doc-> getElementsByTagName(p);
while($ list-> length> 0){
$ p = $ list-> item(0);
$ p-> parentNode-> removeChild($ p);
}
I'm having a problem with a script I've been using for some time and it worked for me until I hit this problem.
I have a script with which I'd like to delete all p html tags from a html source code. The script does work partly because it only removes some of the p tags, but leaves some out.
I don't understand why it does that.
$doc = new DOMDocument();
$a = <<<FAIL
<html><body>
<div style="clear:both"></div>
<p class="articletitle">hoo</p>
<p class="articletext">hmmm</p>
<p class="articletext">hmmmm</p>
<p align="center"></p>
</body></html>
FAIL;
$doc->loadHTML($a);
$list = $doc->getElementsByTagName("p");
foreach ($list as $l) {
$l->parentNode->removeChild($l);
$c++;
}
echo $doc->saveHTML() . $c;
the script returns
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body>
<div style="clear:both"></div>
<p class="articletext">hmmm</p>
<p align="center"></p>
leaving out two p tags...
Can you please help me to find out why it's skipping some tags
解决方案
Try this way:
$doc->loadHTML($a);
$list = $doc->getElementsByTagName("p");
while ($list->length > 0) {
$p = $list->item(0);
$p->parentNode->removeChild($p);
}
这篇关于DOM php按标签名称删除所有标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文