Extracting specific parts of a HTML-File with PHP Simple HTML DOM Parser -
i've got html-file several tables try extract link , image part. i'm using php simple html dom parser.
here's html-file 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 need 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 php part – but doesn't work way want it...
<?php // include library include('simple_html_dom.php'); // retrieve dom given url $html = file_get_html('http://google.com'); // find images & links foreach($html->find('img') $imgelement) foreach($html->find('a') $aelement) echo '<a href="' . $aelement->href . '"><img src="' . $imgelement->src . '" /><br>'; ?>
i think want find img within tag :
foreach($html->find('a img') $imgelement) { echo '<a href="' . $imgelement->parent()->href . '"><img src="' .$imgelement->src .'" /><br>'; }
Comments
Post a Comment