Created
June 26, 2012 14:49
-
-
Save jmoz/2996220 to your computer and use it in GitHub Desktop.
DomXpath example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author James Morris <[email protected]> | |
*/ | |
$html = <<<'EOF' | |
<html> | |
<body> | |
<h1>Foo</h1> | |
<div id="content"> | |
<div class="foo"> | |
<div><img class="fooimage" src="http://foo.com/bar.png" /></div> | |
<p class="description">Foo bar</p> | |
</div> | |
<div class="foo"> | |
<div><img class="fooimage" src="http://foo.com/baz.png" /></div> | |
<p class="description">Baz bat</p> | |
</div> | |
</div> | |
</body> | |
</html> | |
EOF; | |
$doc = new DomDocument(); | |
$doc->loadHTML($html); | |
$xpath = new DomXpath($doc); | |
$entries = $xpath->query("//div[@id='content']/div[@class='foo']"); | |
$results = array(); | |
foreach ($entries as $entry) { | |
// pass in the $entry node as the context node, the the query is relative to it | |
$node = $xpath->query("div/img[@class='fooimage']/attribute::src", $entry); // returns a DOMNodeList | |
$result['image_src'] = $node->item(0)->value; // get the first node in the list which is a DOMAttr | |
$node = $xpath->query("p[@class='description']", $entry); | |
$result['desc'] = $node->item(0)->nodeValue; | |
$results[] = $result; | |
} | |
print_r($results); |
bunkernine
commented
Oct 13, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment