2
는 :PHP DOMDocument를 사용하여 속성을 제거하는 방법은 무엇입니까? XML이 조각으로
<my_xml>
<entities>
<image url="lalala.com/img.jpg" id="img1" />
<image url="trololo.com/img.jpg" id="img2" />
</entities>
</my_xml>
나는 이미지 태그 내의 모든 속성을 제거해야합니다. 그래서,이 작업을 완료했습니다
<?php
$article = <<<XML
<my_xml>
<entities>
<image url="lalala.com/img.jpg" id="img1" />
<image url="trololo.com/img.jpg" id="img2" />
</entities>
</my_xml>
XML;
$doc = new DOMDocument();
$doc->loadXML($article);
$dom_article = $doc->documentElement;
$entities = $dom_article->getElementsByTagName("entities");
foreach($entities->item(0)->childNodes as $child){ // get the image tags
foreach($child->attributes as $att){ // get the attributes
$child->removeAttributeNode($att); //remove the attribute
}
}
?>
나는 foreach는 블록 내 속성에서 제거하려고 할 때 내부 포인터를 분실하고 속성을 모두 삭제하지 않습니다처럼 어떻게 든, 그것은 보인다.
다른 방법이 있습니까? 사전에
감사합니다.
빙고! 첫 번째 방법을 사용하고 있습니다. 고마워요! (다른 두 사람도 잘 작동합니다) – romulodl