Created
September 10, 2015 16:56
-
-
Save veb/78879ad9b7a6022dd29e to your computer and use it in GitHub Desktop.
articles-6
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 | |
$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