PHP DomDocument:如何删除div? [英] PHP DomDocument : How remove a div?
本文介绍了PHP DomDocument:如何删除div?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
$doc = new DOMDocument();
$doc->loadHTML(stripslashes($sContent));
// si le meta existe alors il y a un sondage
if($doc->getElementById('meta') != null){
$nombreMeta = $doc->getElementById('meta')->nodeValue;
$numSondage = $doc->getElementById('meta')->getAttribute('class');
for($i=0;$i<$nombreMeta;$i++){
$meta = $meta."0,";
}
$meta = substr($meta,0,-1);
$divMeta = $doc->getElementById('meta');
$oldchapter = $doc->removeChild($divMeta);
$oHead = $doc->getElementsByTagName('head')->item(0);
$oMeta2 = $doc->createElement('meta');
$oMeta2->setAttribute('name',"metapoll".$numSondage);
$oMeta2->setAttribute('content',$meta);
$oHead->insertBefore($oMeta2, $oHead->firstChild);
$sContent = $doc->saveHTML();
}
我正在尝试使用 id =删除div meta
($ doc-> getElementById('meta'))
,但我不知道为什么它不起作用。
I'm trying to remove div with id="meta"
($doc->getElementById('meta'))
, but I don't know why it doesn't work.
我已经测试了很多东西,例如:
I have tested lots of things like :
$metmet = $doc->documentElement;
$divMeta = $metmet->getElementById('meta')->item(0);
$metmet->removeChild($divMeta);
但这不起作用。有任何想法吗?
But it doesn't work. Any ideas ?
推荐答案
removeChild
方法会从节点。 div不能是 document
对象的子元素,只有< html>
可以(在HTML文档中) )。
The removeChild
method removes a child element from a node. A div cannot be a child element of the document
object, only <html>
can be (in an HTML document).
已找到要删除的div,则需要获取其父节点。然后调用 removeChild
。
Having found the div you want to remove, you need to fetch its parent node. Then call removeChild
on that.
$divMeta->parentNode->removeChild($divMeta)
这篇关于PHP DomDocument:如何删除div?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文