Skip to content

Instantly share code, notes, and snippets.

@veb
Created September 10, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veb/78879ad9b7a6022dd29e to your computer and use it in GitHub Desktop.
Save veb/78879ad9b7a6022dd29e to your computer and use it in GitHub Desktop.
articles-6
<?php
$dom = new DOMDocument();
@$dom->loadHTML($content);
$tempDom = new DOMDocument();
$xpath = new DOMXPath($dom);
$container = $xpath->query("//div[@id='container']");
foreach ( $container as $item ) {
$tempDom->appendChild($tempDom->importNode($item,true));
}
$tempDom->saveHTML();
$peopleXpath = new DOMXPath($tempDom);
$peopleDiv = $peopleXpath->query("div[@id='person']");
$results = array();
foreach ($peopleDiv as $people) {
$newDom = new DOMDocument;
$newDom->appendChild($newDom->importNode($people,true));
$personXpath = new DOMXPath($newDom);
$name = trim($personXpath->query("p[1]/text()")->item(0)->nodeValue);
$age = trim($resultXpath->query("p[2]/text()")->item(0)->nodeValue);
$weight = trim($resultXpath->query("p[3]/text()")->item(0)->nodeValue);
$image = trim($resultXpath->query("img[1]/@src")->item(0)->nodeValue);
$results[] = array(
'name' => $name,
'age' => $age,
'weight' => $weight,
'image' => $image,
);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment