1

Is there any Regex to Read the <td> contents from the following Table, note that there are many similar tables so i want only to read the following table contents.

I want to READ data from 3,4,5 TD from all <TR>

My regex looks like following but doesnt work

$match = preg_replace('~<td width="80" bgcolor="#F3F3E4" align="left">&nbsp;<a onmouseout="ChangeImage(AE1,1)" onmouseover="ChangeImage(AE1,0)" href="/charts/livegold.html">GOLD</a></td>#[a-z0-9]{6}~i','',$match[3]);
echo '<table><tr>' . $match . '</tr></table>';

the table is as follows

 <table width="540" cellspacing="1" cellpadding="0" border="0" align="center">
  <tbody><tr>
    <td width="16" bgcolor="#000000" align="center">&nbsp;</td>
    <td width="80" bgcolor="#000000" align="center"><font size="1" face="Arial, Helvetica, sans-serif" color="#FFFFFF">www.kitco.com</font></td>
    <td width="369" bgcolor="#000000" align="center" colspan="5"><p class="white">The World Spot Price - Asia/Europe/NY markets</p></td>
    <td width="73" bgcolor="#000000" align="right"><a href="/market/lights.html"><img width="39" vspace="0" hspace="0" height="17" border="0" alt="light" src="/images/lightgreen.gif"></a></td>
  </tr>
  <tr>
    <td width="16" bgcolor="#000000" align="center">&nbsp;</td>
    <td width="522" bgcolor="#F3F3E4" align="center" colspan="7"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><b><font color="GREEN">MARKET IS OPEN</font><br>(Will close in 17 hrs. 41 mins.)<!-- 1486.00--></b></font></td>
  </tr>
  <tr bgcolor="#F3F3E4">
    <td width="16" bgcolor="#000000" align="center">&nbsp;</td>
    <td width="80" bgcolor="#CCCC99" align="center">Metals</td>
    <td width="80" bgcolor="#CCCC99" align="center">Date</td>
    <td width="80" bgcolor="#CCCC99" align="center">Time (EST)</td>
    <td width="68" bgcolor="#CCCC99" align="center">Bid</td>
    <td width="68" bgcolor="#CCCC99" align="center">Ask</td>
    <td width="146" bgcolor="#CCCC99" align="center" colspan="2">Change from NY Close</td>
  </tr>
  <tr bgcolor="#F3F3E4">
    <td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE1','1')" onmouseover="ChangeImage('AE1','0')" href="/charts/livegold.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Gold Charts" name="AE1" src="http://www.kitco.com/images/graph_down.gif"></a></td>
    <td width="80" bgcolor="#F3F3E4" align="left">&nbsp;<a onmouseout="ChangeImage('AE1','1')" onmouseover="ChangeImage('AE1','0')" href="/charts/livegold.html">GOLD</a></td>
    <td width="80" align="center">06/04/2013</td>
    <td width="80" align="center">23:34</td>
    <td width="68" align="center">1405.50</td>
    <td width="68" align="center">1406.50</td>
    <td width="73" align="center"><p class="spotgreen">+5.50</p></td>
    <td width="73" align="center"><p class="spotgreen">+0.39%</p></td>
  </tr>
  <tr bgcolor="#F3F3E4">
    <td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE2','1')" onmouseover="ChangeImage('AE2','0')" href="/charts/livesilver.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Silver Charts" name="AE2" src="http://www.kitco.com/images/graph_down.gif"></a></td>
    <td width="80" align="left">&nbsp;<a onmouseout="ChangeImage('AE2','1')" onmouseover="ChangeImage('AE2','0')" href="/charts/livesilver.html">SILVER</a></td>
    <td width="80" align="center">06/04/2013</td>
    <td width="80" align="center">23:34</td>
    <td width="68" align="center">22.59</td>
    <td width="68" align="center">22.69</td>
    <td width="73" align="center"><p class="spotgreen">+0.05</p></td>
    <td width="73" align="center"><p class="spotgreen">+0.20%</p></td>
  </tr>
  <tr bgcolor="#F3F3E4">
    <td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE3','1')" onmouseover="ChangeImage('AE3','0')" href="/charts/liveplatinum.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Platinum Charts" name="AE3" src="http://www.kitco.com/images/graph_down.gif"></a></td>
    <td width="80" align="left"><p>&nbsp;<a onmouseout="ChangeImage('AE3','1')" onmouseover="ChangeImage('AE3','0')" href="/charts/liveplatinum.html">PLATINUM</a></p></td>
    <td width="80" align="center">06/04/2013</td>
    <td width="80" align="center">23:34</td>
    <td width="68" align="center">1501.00</td>
    <td width="68" align="center">1509.00</td>
    <td width="73" align="center"><p class="spotgreen">+9.00</p></td>
    <td width="73" align="center"><p class="spotgreen">+0.60%</p></td>
  </tr>
  <tr bgcolor="#F3F3E4">
    <td width="16" bgcolor="#000000" align="center"><a onmouseout="ChangeImage('AE4','1')" onmouseover="ChangeImage('AE4','0')" href="/charts/livepalladium.html"><img width="16" vspace="0" hspace="0" height="13" border="0" alt="Palladium Charts" name="AE4" src="http://www.kitco.com/images/graph_down.gif"></a></td>
    <td width="80" align="left">&nbsp;<a onmouseout="ChangeImage('AE4','1')" onmouseover="ChangeImage('AE4','0')" href="/charts/livepalladium.html">PALLADIUM</a></td>
    <td width="80" align="center">06/04/2013</td>
    <td width="80" align="center">23:25</td>
    <td width="68" align="center">755.00</td>
    <td width="68" align="center">761.00</td>
    <td width="73" align="center"><p class="spotgreen">+6.00</p></td>
    <td width="73" align="center"><p class="spotgreen">+0.80%</p></td>
  </tr>
</tbody></table>    

Here is the data i want to Extract

enter image description here

7
  • "Is there any Regex to Read..." Yeah, maybe. We can help you with your current regex attempt... What didn't work? Check this famous answer too stackoverflow.com/questions/1732348/…, just in case. Commented Jun 5, 2013 at 4:19
  • @elclanrs added current regex Commented Jun 5, 2013 at 4:21
  • @user580950 - which data you want to extract? give an example Commented Jun 5, 2013 at 4:27
  • +1. Maybe somebody can help. How about using a DOM parser like the answer below suggested? Commented Jun 5, 2013 at 4:28
  • @RobinVanPersi I want to read data from 3,4,5 TD from all TRs Commented Jun 5, 2013 at 4:35

2 Answers 2

2

ok here is the solution with Regex:

$patt = "/<td[^>]*width=['\"]68['\"][^>]*>([0-9\.]+)<\/td>\s*<td[^>]*width=['\"]68['\"][^>]*>([0-9\.]+)<\/td>/i";
if(preg_match_all($patt, $html, $matches))
{
   //print all records
   //print_r($matches);

   for($i=0; $i<count($matches[1]); $i++)
   {

       echo "Bid: ".$matches[1][$i].", Ask: ".$matches[2][$i]."\n";

   }

}
Sign up to request clarification or add additional context in comments.

1 Comment

Great it displays Bid,Ask value from Gold, Silver Rows but not from PLATINUM, PALLADIUM please advise
0

You can use some HTML parser. For PHP there is one http://simplehtmldom.sourceforge.net/

Once the DOM has been loaded into library, get through the TABLE element and iterate through each TR element.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.