I've got a HTML-File with several tables from which I try to extract the link and image part. I'm using the PHP Simple HTML DOM Parser.
Here's the HTML-File to parse:
<h1>Title</h1>
<p>Text</p>
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr><td>
<a href="http://www.google.com/some_url">
<img width="100" height="100" border="0" src="http://google.com/some_image.jpg"/>
</a>
</td></tr>
</tbody>
</table>
<h2>Title</h2>
<p>Text</p>
<table cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr><td>
<a href="http://www.google.com/this_url">
<img width="100" height="100" border="0" src="http://google.com/this_image.jpg"/>
</a>
</td></tr>
</tbody>
</table>
<p>Text</p>
<p>Text</p>
And what I need as an output:
<a href="http://www.google.com/some_url">
<img width="100" height="100" border="0" src="http://google.com/some_image.jpg"/>
</a>
<a href="http://www.google.com/this_url">
<img width="100" height="100" border="0" src="http://google.com/this_image.jpg"/>
</a>
Here's the PHP part – but doesn't work the way i want it...
<?php
// Include the library
include('simple_html_dom.php');
// Retrieve the DOM from a given URL
$html = file_get_html('http://google.com');
// Find all images & links
foreach($html->find('img') as $IMGelement)
foreach($html->find('a') as $Aelement)
echo '<a href="' . $Aelement->href . '"><img src="' . $IMGelement->src . '" /><br>';
?>