I need to grab a URL using simple_html_dom based on the end of the URL. The URL has no specific class to make it unique. The only thing unique about it is that it ends with a specific set of numbers. I just cannot figure out the proper syntax to grab that specific URL and then print it.
Any help?
EXAMPLE:
<table class="findList">
<tr class="findResult odd"> <td class="primary_photo"> <a href="/title/tt0080487/?ref_=fn_al_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg" height="44" width="32" /></a> </td>
That is the code for the beginning of the table. That first href is the one I want to grab. The table continues with more links, etc, but that's not relevant to what I want.
For the first a with a href ending in 1:
$dom->find('a[href$="1"]', 0);
You can simply use DOMdocument
<?php
$html = '
<table class="findList">
<tr class="findResult odd">
<td class="primary_photo">
<a href="/title/tt0080487/?ref_=fn_al_tt_1" ><img src="http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg" height="44" width="32" /></a>
</td>
';
$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
if($td->getAttribute('class') == 'primary_photo'){
$a = $td->getElementsByTagName('a')->item(0)->getAttribute('href');
}
}
echo $a; // title/tt0080487/?ref_=fn_al_tt_1
//Or if your looking to get the img tag
$dom = new DOMDocument();
@$dom->loadHTML($html);
foreach($dom->getElementsByTagName('td') as $td) {
if($td->getAttribute('class') == 'primary_photo'){
$a = $td->getElementsByTagName('img')->item(0)->getAttribute('src');
}
}
echo $a; // http://ia.media-imdb.com/images/M/MV5BNzk2OTE2NjYxNF5BMl5BanBnXkFtZTYwMjYwNDQ5._V1_SY44_CR0,0,32,44_.jpg
?>
Assuming you have your html in a file called "tables.html", this will work. It reads the file, finds all the 'a' links, puts them into a array, and the first one ($anchors[0]) is the one you want. Then you get the href from it with $anchors[0]->href.
$html = new simple_html_dom();
$html->load_file('tables.html');
$anchors = $html->find("a");
echo $anchors[0]->href;