문제

This was previous another question, but we won't talk about that. I am isolating 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 //.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top