I am trying to get the contents from span, class, div from external other domain to mine personal website. The source code of the website http://www.example.com is
<div id="h_gold" class="h_metal">
<div class="hm_inside">
<div class="hm_title">Gold</div>
<span class="arr_price_down">- 13.42 <img src="images/downarrow.jpg" alt="down" /></span>
<div class="clear"></div>
<div class="hm_cad">USD</div>
<div class="hm_bottom">
<div> Bid<br />
<span>$1,749.20</span> </div>
<div class="ask"> Ask<br />
<span>$1,750.20</span> </div>
</div>
<!-- .hm_bottom-->
</div>
<!-- hm_inside-->
</div>
<!-- h_metal-->
<div id="h_silver" class="h_metal">
<div class="hm_inside">
<div class="hm_title">Silver</div>
<span class="arr_price_down">- 0.54 <img src="images/downarrow.jpg" alt="down" /></span>
<div class="clear"></div>
<div class="hm_cad">USD</div>
<div class="hm_bottom">
<div> Bid<br />
<span>$33.40</span> </div>
<div class="ask"> Ask<br />
<span>$33.50</span> </div>
</div>
<!-- .hm_bottom-->
</div>
<!-- hm_inside-->
</div>
<!-- h_metal-->
I have a problem here.. i tried the following code.. but it doesnt loop, I only get the contents from the first < div class=”hm_bottom” > , rests are not in the output,
<?php
function get_string_between($string, $start, $end)
{
$string = " ".$string;
$ini = strpos($string,$start);
if ($ini == 0)
return "";
$ini += strlen($start);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
$data = file_get_contents("http://www.example.com/ajax.php?metal_cur=USD");
$pricediv = get_string_between($data, '<div class="hm_bottom">', '</span>');
$pricetext = strip_tags($pricediv);
echo $pricetext;
?>
Secondly, i searched stachoverflow again, i found one solved, but doesnt perfectly matches my needs. the output is kinda okay, but i just want numerical data from the output to be placed in the particular div id i define.
<?php
$page = file_get_contents('http://www.example.com/ajax.php?metal_cur=USD');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
// Loop through the DIVs looking for one withan id of "content"
// Then echo out its contents (pardon the pun)
if ($div->getAttribute('class') === 'hm_bottom') {
echo $div->nodeValue;
}
}
?>
the above code is from second search from stackoverflow
You can use str_get_html or file_get_html as the case may be
Output