This question already has an answer here:
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"> </td>
<td align="center" bgcolor="#000000" colspan="8"><p class="white">New
York Spot Price</td>
<td></td>
<td align="right" bgcolor="#000000"> </td>
</tr>
<tr bgcolor="#F3F3E4">
<td bgcolor="#000000" width="31" nowrap></td>
<td bgcolor="#000000"> </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"> </td>
</tr>
<tr bgcolor="#F3F3E4" align="center">
<td bgcolor="#000000"> </td>
<td bgcolor="#000000"> </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> </td>
<td bgcolor="#000000" nowrap> </td>
<td align="left"><p> <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> </td>
<td bgcolor="#000000"> </td>
<td align="left"><p> <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> </td>
<td bgcolor="#000000"> </td>
<td align="left"><p> <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> </td>
<td bgcolor="#000000"> </td>
<td align="left"><p> <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>
</div>
Try this:
$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);
I got the following:
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
)
)
I really wonder why you want to use regex for it. Not sure if you really have to, if so this answer wont be any good for you. But you can easily solve this problem with jquery. In jquery you can use .eq()
function to get this done.
$('table tr').eq(5).text()
Made an example here: http://jsfiddle.net/a5Yyf/
Hope its any good to you :) good luck