-2

Is there any Regex to Read the 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 All the Rows from Row number 5,6,7 <TR>

My regex looks like following but doesnt work

$match = preg_replace('~<tr align="center" bgcolor="#F3F3E4">">GOLD</a></td>#[a-z0-9]{6}~i','',$match[3]);
echo '<table><tr>' . $match . '</tr></table>';

MY HTML code looks like this

<table width="571" border="0" cellspacing="0" cellpadding="0"><tr bgcolor="#000000"><td><table border="0" cellspacing="1" cellpadding="0" align="center" width="571">

              <tr>
                <td bgcolor="#000000">&nbsp;</td>
                <td align="center" bgcolor="#000000" colspan="8"><p class="white">New 
                    York Spot Price</td>
                <td></td>
                <td align="right" bgcolor="#000000">&nbsp;</td>
              </tr>
              <tr bgcolor="#F3F3E4">
                <td bgcolor="#000000" width="31" nowrap></td>
                <td bgcolor="#000000">&nbsp;</td>
                <td align="center" colspan="7" bgcolor="#F3F3E4"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><font color=RED>MARKET IS CLOSED</font><BR>(Will open in 5 hrs. 25 mins.)</b></font></td>
                <td colspan="2" align="center" bgcolor="#F3F3E4">&nbsp;</td>
              </tr>
              <tr bgcolor="#F3F3E4" align="center">
                <td bgcolor="#000000">&nbsp;</td>
                <td bgcolor="#000000">&nbsp;</td>
                <td bgcolor="#CCCC99"><p>Metals</td>
                <td bgcolor="#CCCC99"><p>Date</td>
                <td bgcolor="#CCCC99"><p>Time<br>
                    (EST)</td>
                <td bgcolor="#CCCC99"><p>Bid</td>
                <td bgcolor="#CCCC99"><p>Ask</td>
                <td colspan="2" bgcolor="#CCCC99"><p>Change</td>
                <td bgcolor="#CCCC99"><p>Low</td>
                <td bgcolor="#CCCC99"><p>High</td>
              </tr>
              <tr bgcolor="#F3F3E4" align="center">
                <td bgcolor="#000000" width="31" nowrap>&nbsp;</td>
                <td bgcolor="#000000" nowrap>&nbsp;</td>
                <td align="left"><p>&nbsp;<a href="/charts/livegoldnewyork.html" onMouseOver="ChangeImage('NY1','0')" onMouseOut="ChangeImage('NY1','1')">GOLD</a></td>
                <td><p>07/10/2013</td>
                <td><p>17:15</td>
                <td><p>1262.90</td>
                <td><p>1263.90</td>
                <td><p class=spotgreen>+12.20</p></td>
                <td><p class=spotgreen>+0.98%</p></td>
                <td><p>1247.10</td>
                <td><p>1268.40</td>
              </tr>

              <tr align="center" bgcolor="#F3F3E4">
                <td bgcolor="#000000" width="31" nowrap>&nbsp;</td>
                <td bgcolor="#000000">&nbsp;</td>
                <td align="left"><p>&nbsp;<a href="/charts/livesilver.html#ny" onMouseOver="ChangeImage('NY2','0')" onMouseOut="ChangeImage('NY2','1')">SILVER</a></td>
                <td><p>07/10/2013</td>
                <td><p>17:15</td>
                <td><p>19.47</td>
                <td><p>19.57</td>
                <td><p class=spotgreen>+0.20</p></td>
                <td bgcolor="#F3F3E4"><p class=spotgreen>+1.06%</p></td>
                <td><p>19.03</td>
                <td><p>19.69</td>
              </tr>
              <tr align="center" bgcolor="#F3F3E4">
                <td bgcolor="#000000" width="31" nowrap>&nbsp;</td>
                <td bgcolor="#000000">&nbsp;</td>
                <td align="left"><p>&nbsp;<a href="/charts/liveplatinum.html" onMouseOver="ChangeImage('NY3','0')" onMouseOut="ChangeImage('NY3','1')">PLATINUM</a></td>
                <td><p>07/10/2013</td>
                <td><p>17:13</td>
                <td><p>1372.00</td>
                <td><p>1377.00</td>
                <td><p class=spotgreen>+6.00</p></td>
                <td><p class=spotgreen>+0.44%</p></td>
                <td><p>1358.00</td>
                <td><p>1383.00</td>
              </tr>
              <tr align="center" bgcolor="#F3F3E4">
                <td bgcolor="#000000" width="31" nowrap>&nbsp;</td>
                <td bgcolor="#000000">&nbsp;</td>
                <td align="left"><p>&nbsp;<a href="/charts/livepalladium.html" onMouseOver="ChangeImage('NY4','0')" onMouseOut="ChangeImage('NY4','1')">PALLADIUM</a></td>
                <td><p>07/10/2013</td>
                <td><p>17:11</td>
                <td><p>714.00</td>
                <td><p>719.00</td>
                <td><p class=spotgreen>+16.00</p></td>
                <td><p class=spotgreen>+2.29%</p></td>
                <td><p>707.00</td>
                <td><p>721.00</td>
              </tr>
            </table></td></tr></table>
4

2 回答 2

0

我真的很想知道你为什么要使用正则表达式。不确定你是否真的必须这样做,如果是这样,这个答案对你没有任何好处。但是你可以用jquery轻松解决这个问题。在 jquery 中,您可以使用.eq()函数来完成此操作。

$('table tr').eq(5).text()

在这里做了一个例子:http: //jsfiddle.net/a5Yyf/

希望它对你有好处:)祝你好运

于 2013-07-11T07:42:57.350 回答
0

尝试这个:

$dom = new DOMDocument();
@$dom->loadHTML($html);
$tr = $dom->getElementsByTagName('tr');
$out = array();
foreach ($tr as $key => $value){
    if ($key > 4 && $key < 8){
        $p = $value->getElementsByTagName('p');
        foreach ($p as $key2 => $value2){
            $out[$key][] = $value2->nodeValue;
        }
    }
}
var_dump($out);

我得到以下信息:

Array
(
    [5] => Array
        (
            [0] =>  SILVER
            [1] => 07/10/2013
            [2] => 17:15
            [3] => 19.47
            [4] => 19.57
            [5] => +0.20
            [6] => +1.06%
            [7] => 19.03
            [8] => 19.69
        )

    [6] => Array
        (
            [0] =>  PLATINUM
            [1] => 07/10/2013
            [2] => 17:13
            [3] => 1372.00
            [4] => 1377.00
            [5] => +6.00
            [6] => +0.44%
            [7] => 1358.00
            [8] => 1383.00
        )

    [7] => Array
        (
            [0] =>  PALLADIUM
            [1] => 07/10/2013
            [2] => 17:11
            [3] => 714.00
            [4] => 719.00
            [5] => +16.00
            [6] => +2.29%
            [7] => 707.00
            [8] => 721.00
        )

)
于 2013-07-11T07:42:05.453 回答