I'm trying to create a PHP script that uses XPath to "scrape" all the thumbnails, with corresponding links, from an HTML document. The script is getting the correct data, it's just not outputting it correctly. I'm not a PHP person, so I was trying to piece things together and I'm not getting anywhere. The code below displays all the links and then all the image urls. $hrefs = $xpath->query("//a[@class='blah']/@href"); $thumbnails = $xpath->query("//img[@class='blah']/@src"); foreach ($hrefs as $h) { $url = $h->nodeValue; echo $url; } foreach ($thumbnails as $t) { $thumb = $t->nodeValue; echo $thumb; } Code (markup): How can I get the output to display like a normal thumbnail for each one? I tried the code below, but that doesn't work. echo "<a href=" . $url . "><img src=" . $thumb . " /></a><br />"; Code (markup):
$hrefs = $xpath->query("//a[@class='blah']/@href"); $thumbnails = $xpath->query("//img[@class='blah']/@src"); foreach ($hrefs as $h) { $url = $h->nodeValue; echo $url; } foreach ($thumbnails as $t) { $thumb = $t->nodeValue; echo $thumb; } Code (markup): Output: http://www.site1.comhttp://www.site2.comhttp://www.site3.comhttp://www.site4.comhttp://www.site5.comhttp://www.site1.com/images/1.jpghttp://www.site2.com/images/2.jpghttp://www.site3.com/images/3.jpghttp://www.site4.com/images/4.jpghttp://www.site5.com/images/5.jpg Code (markup): Desired Output: <a href="http://www.site1.com"><img src="http://www.site1.com/images/1.jpg"></a><br /> <a href="http://www.site2.com"><img src="http://www.site2.com/images/2.jpg"></a><br /> ... Code (markup):
I haven debugged this code... But this may work <?php $url_n_img = array( 0 => array( 'url' => '', 'img' => '' ) ); $html = file_get_contents('http://www.intechgrity.com/2010/05/single-indexphp-php-script-to-load.html'); $dom = new DOMDocument(); @$dom->loadHTML($html); $xpath = new DOMXPath($dom); $hrefs = $xpath->query("//a[@class='blah']/@href"); $thumbnails = $xpath->query("//img[@class='blah']/@src"); $i=0; $j=0; foreach($hrefs as $h) { $url_n_img[$i]['url'] = $h->nodeValue; $i++; } foreach($thumbnails as $t) { $url_n_img[$j]['img'] = $t->nodeValue; $j++; } //print_r($url_n_img); foreach($url_n_img as $u) { echo '<a href="'.$u['url'].'"><img src="'.$u['img'].'" /></a>'; } ?> PHP: What we do is, store the hrefs and thumbs in another array and fetch the values later with our desired markup! Do let me know if it works!
You are a genius! Does anyone else have any input? I know sometimes there's a better way to do things. I don't know much about PHP though.
Yes swashata is a genious, We can modify the code slightly to display only ten thumbnails. Just change the value of $end to the desired number of thumbnails. Of course if the value of $end is more than the number of thumbnails then it will only display the total number thumbnails. <?php $url_n_img = array( 0 => array( 'url' => '', 'img' => '' ) ); $html = file_get_contents('http://www.intechgrity.com/2010/05/single-indexphp-php-script-to-load.html'); $dom = new DOMDocument(); @$dom->loadHTML($html); $xpath = new DOMXPath($dom); $hrefs = $xpath->query("//a[@class='blah']/@href"); $thumbnails = $xpath->query("//img[@class='blah']/@src"); $i=0; $j=0; foreach($hrefs as $h) { $url_n_img[$i]['url'] = $h->nodeValue; $i++; } foreach($thumbnails as $t) { $url_n_img[$j]['img'] = $t->nodeValue; $j++; } $start=0; $end=10; // How many thumbnails do you want? // foreach($url_n_img as $u) { if($start<$end){echo '<a href="'.$u['url'].'"><img src="'.$u['img'].'" /></a>';} $start++; } ?> PHP: