开发者

Correctly deleting child elements in PHP using DOMDocument and DOMXPath

This was previous another question, but we won't talk about that. I am i开发者_JAVA百科solating a number of sections in a third party HTML document. When matching some, I need to remove certain tags from the result. The code I found for this on SO was:

$name = $xpath->query("//div[@class='leftColBig']//h3")->item(0);
// remove <span>
foreach($xpath->query("//span", $name) as $node)
    $node->parentNode->removeChild($node);

This has the unfortunate side effect of not just deleting the child from $name, but the entire DOMDocument :( How can I isolate the removeChild just to the section I found using the query.


Instead of:

$xpath->query("//span", $name)

Do:

$xpath->query("span", $name)

//nodename matches all nodes no matter what their parent is. $contextnode is ignored when your query starts with //.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜