roshan thapa
roshan thapa

Reputation: 110

get contents from div class span from other website

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 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

Upvotes: 1

Views: 7190

Answers (1)

Baba
Baba

Reputation: 95141

You can use str_get_html or file_get_html as the case may be

$html = str_get_html($html);
$amounts = array();
foreach ( $html->find("div[class=hm_bottom] span") as $span ) {
    $amount = trim($span->plaintext);
    if (strpos($amount, "\$") === 0)
        $amounts[] = $amount;
}

var_dump($amounts);

Output

array
  0 => string '$1,749.20' (length=9)
  1 => string '$1,750.20' (length=9)
  2 => string '$33.40' (length=6)
  3 => string '$33.50' (length=6)

Upvotes: 3

Related Questions