1

是否有任何正则表达式可以<td>从以下内容中读取内容Table,请注意有很多类似的内容,tables所以我只想阅读以下table内容。

我想TD从所有的 3,4,5 中读取数据<TR>

我的正则表达式看起来像以下但不起作用

$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>';

表格如下

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

这是我要提取的数据

在此处输入图像描述

4

2 回答 2

2

好的,这是使用正则表达式的解决方案:

$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";

   }

}
于 2013-06-05T05:22:15.380 回答
0

您可以使用一些 HTML 解析器。对于 PHP,有一个http://simplehtmldom.sourceforge.net/

一旦 DOM 被加载到库中,TABLE遍历元素并遍历每个TR元素。

于 2013-06-05T04:20:08.527 回答