I'm trying to use a DOM parser for multiple links and then compare for 2 pairs of values. Could someone help me on where I went wrong? Is it not possible for me to do the comparison for the @class="badge-item-img"
?
EDIT I should mention that the first foreach works but when trying to find the second one there is no results shown.
<?php
// Init the '$url_array' array.
$url_array = array();
$url_array[] = 'http://www.reddit.com/r/funny';
$url_array[] = 'http://www.9gag.com/';
// Init the return '$ret' array.
$ret = array();
// Roll through the '$url_array' array.
foreach ($url_array as $url_value) {
$html = file_get_contents($url_value);
$dom = new DOMDocument();
$dom2 = new DOMDocument();
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$xpath2 = new DOMXPath($dom2);
$hyperlinks = $xpath->evaluate('//a[@class="thumbnail "]');
$hyperlinks2 = $xpath2->evaluate('//a[@class="badge-item-img"]');
foreach($hyperlinks as $hyperlink) {
if(strpos($hyperlink->getAttribute('href'), 'http://i.imgur.com/') !== FALSE){
$ret[] = "<img style='padding-left:30%' width=\"500\" src=\"" . $hyperlink->getAttribute('href') . "\" alt=\"\" />"
. "<br>"
. "<br>"
. "<br>"
;
}
foreach($hyperlinks2 as $hyperlinker) {
$ret[] = "<img style='padding-left:30%' width=\"500\" src=\"" . $hyperlinker->getAttribute('href') . "\" alt=\"\" />"
. "<br>"
. "<br>"
. "<br>"
;
}
}
}
// Roll through the '$ret' array.
foreach($ret as $ret_value) {
echo $ret_value;