-1

I am parsing a html page and i want certain value which changes at regular interval. But i dont know how to achieve it.

<table class="priceTable">
<tr>
    <th colspan="3">Generic Gold Price By Carat/Karat - Today  - Today <br>
    Mon, Aug 3rd, 2015<br>Gold Price Today Per Gram - Current Gold Price in Indian Rupees</th>
</tr>

<tr>    
    <th>22 Karat Today</th>
    <th>22 Karat Yesterday</th> 
    <th>Price Change</th>   

</tr>
<tr>    
    <td>1g = Rs. 2377.00</td>
    <td>1g = Rs. 2377.00</td>   
    <td>

            0       <img SRC="images/green.gif"  alt="India Gold Rate Price Difference Today">
        </td>   
</tr>
<tr>    
    <th>24 Karat (Pure Gold) Today</th>
    <th>24 Karat (Pure Gold) Yesterday</th> 
    <th>Price Change</th>   
</tr>
<tr>    
    <td>1g = Rs. 2541.00</td>
    <td>1g = Rs. 2541.00</td>   
    <td>
            0       <img SRC="images/green.gif" alt="India Gold Rate Price Difference Today">
        </td>   
</tr>
</table>

I need the value of 1g 22 karat today and 24 karat today. My Code

$regex='/<table class="priceTable">
<tbody><tr>
    <th colspan="3">Generic Gold Price By Carat\/Karat - Today  - Today <br>
    Mon, Aug 3rd, 2015<br>Gold Price Today Per Gram - Current Gold Price in Indian Rupees<\/th>
<\/tr>

<tr>    
    <th>22 Karat Today<\/th>
    <th>22 Karat Yesterday<\/th>    
    <th>Price Change<\/th>  

<\/tr>
<tr>    
    <td>(^[a-zA-Z0-9_.-]*$)<\/td>/';



//$regex='/<tr>
//  <td>MUMBAI<br><br><a class="highlightlink" href="http:\/\/mumbai.indiagoldrate.com">Gold Rates in <br>Mumbai - More Info &amp; archives<\/a>
//
//  <br><br><!-- <A class="highlightlink" HREF="mumbai-gold-rate-on-2015-08-03.htm">Gold & Silver Rates in <BR>Mumbai - on 3-Aug-2015<\/A> -->
//  <\/td>
//  <td><table class="innerTable">
//  <tbody><tr>
//      <td>([\w\W]*?)<\/td>/';
preg_match($regex,$data,$match);
$line = $match[0];
echo $line;
echo '<br/>';

Error: Undefined Offset. please help me

Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89

1 Answers1

0

Judging by your regex, you are trying to obtain the node value of the first td tag in each row of the table with class equal to priceTable.

I suggest using DOMDocument with DOMXPath:

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

$xpath = new DOMXPath($dom);
$tds = $xpath->query("//table[@class='priceTable']/tr/td[1]");//th[contains(., 'Karat')]

foreach($tds as $td) { 
   echo $td->nodeValue . "\n";
}

See IDEONE demo

You will get 2 values if you use the input you provided: 1g = Rs. 2377.00 and 1g = Rs. 2541.00.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563