fork download
  1. <?php
  2.  
  3. $html = <<<DOM
  4. <table class="priceTable">
  5. <tr>
  6.   <th colspan="3">Generic Gold Price By Carat/Karat - Today - Today <br>
  7.   Mon, Aug 3rd, 2015<br>Gold Price Today Per Gram - Current Gold Price in Indian Rupees</th>
  8. </tr>
  9.  
  10. <tr>
  11.   <th>22 Karat Today</th>
  12.   <th>22 Karat Yesterday</th>
  13.   <th>Price Change</th>
  14.  
  15. </tr>
  16. <tr>
  17.   <td>1g = Rs. 2377.00</td>
  18.   <td>1g = Rs. 2377.00</td>
  19.   <td>
  20.  
  21.   0 <img SRC="images/green.gif" alt="India Gold Rate Price Difference Today">
  22.   </td>
  23. </tr>
  24. <tr>
  25.   <th>24 Karat (Pure Gold) Today</th>
  26.   <th>24 Karat (Pure Gold) Yesterday</th>
  27.   <th>Price Change</th>
  28. </tr>
  29. <tr>
  30.   <td>1g = Rs. 2541.00</td>
  31.   <td>1g = Rs. 2541.00</td>
  32.   <td>
  33.   0 <img SRC="images/green.gif" alt="India Gold Rate Price Difference Today">
  34.   </td>
  35. </tr>
  36. </table>
  37. DOM;
  38.  
  39. $dom = new DOMDocument('1.0', 'UTF-8');
  40. $dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
  41.  
  42. $xpath = new DOMXPath($dom);
  43. $tds = $xpath->query("//table[@class='priceTable']/tr/td[1]");//th[contains(., 'Karat')]
  44.  
  45. foreach($tds as $td) {
  46. echo $td->nodeValue . "\n";
  47. }
  48.  
  49.  
  50.  
Success #stdin #stdout 0.02s 52432KB
stdin
Standard input is empty
stdout
1g = Rs. 2377.00
1g = Rs. 2541.00