Last active
January 28, 2019 07:10
-
-
Save maietta/70b746c9497364a63b9bfbe36ec74f37 to your computer and use it in GitHub Desktop.
PHP DOM. Find element, get parent, then find sibblings. Build JSON
This file contains hidden or 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 | |
$raw_content = file_get_contents("NBA_example.txt"); | |
$dom = new DOMDocument; | |
$dom->loadHTML("$raw_content"); | |
$block = $dom->getElementById("oddsGridContainer"); | |
$results = []; | |
$assets = $block->getElementsByTagName('script'); | |
for ($i = 0; $i < $assets->length; $i++) { | |
$dataset = []; | |
$resource = $assets->item($i); | |
if ( $resource->hasAttribute('type') ) { | |
if ( $resource->getAttribute('type') == 'application/ld+json' ) { | |
$js = str_replace('
', '', trim($resource->C14N())); | |
$js = everything_in_tags($js, "script"); | |
$dataset = json_decode($js, true); | |
foreach($resource->parentNode->getElementsByTagName('div') as $node) $dataset['values'][] = $node->nodeValue; | |
$results[] = $dataset; | |
} | |
} | |
} | |
/* | |
{ | |
"@context": "http:\/\/schema.org\/", | |
"@type": "SportsEvent", | |
"@id": "3569985", | |
"name": "Phoenix Suns vs L.A. Lakers", | |
"url": "https:\/\/classic.sportsbookreview.com\/betting-odds\/nba-basketball\/phoenix-suns-vs-la-lakers-3569985\/", | |
"startDate": "2019-01-27T21:30:00-05:00", | |
"location": { | |
"@type": "StadiumOrArena", | |
"name": "Staples Center", | |
"image": "http:\/\/www.static-files.com\/www.sbrforum.com\/sbrlogo.png", | |
"address": { | |
"addressLocality": "Los Angeles", | |
"addressRegion": "CA" | |
} | |
}, | |
"values": [ | |
"527", | |
"528" | |
] | |
}, | |
*/ | |
foreach($results as $result) { | |
echo '<img src="'.$result['location']['image'].'" width="150px" />'; | |
echo '<h3><a href="'.$result['url'].'">'. $result['name'] . "</a></h3><br />"; | |
echo '<p>'.$result['location']['address']['addressLocality']. ', ' .$result['location']['address']['addressRegion'].'</p>'; | |
echo "First: " . $result['values'][0] . "<br />"; | |
echo "Second: " . $result['values'][1] . "<br />"; | |
echo '<hr />'; | |
} | |
function everything_in_tags($string, $tagname) { | |
$pattern = "#<\s*?$tagname\b[^>]*>(.*?)</$tagname\b[^>]*>#s"; | |
preg_match($pattern, $string, $matches); | |
return trim($matches[1]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment