-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>
user580950
  • 3,558
  • 12
  • 49
  • 94
  • [Related](http://stackoverflow.com/a/1732454/1607098) – Touki Jul 11 '13 at 07:30
  • Regex everywhere. Sorry, just could not help myself. Regex is not always right tool to solve problems related to strings. – Leri Jul 11 '13 at 07:31
  • 3
    Hi, this is the third low-quality parsing question that you are asking. Please stop. The link I posted in the deleted question gives you everything you need; [here it is again.](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) Also, I notice that your questions are generally of rather low quality. You're asking many things that could be easily looked up in a manual with some effort. Stop being a [help vampire](http://www.slash7.com/pages/vampires) and start helping yourself! – Pekka Jul 11 '13 at 07:35
  • @Pekka웃 Your comments are offensive i have sent an email to admin@stackoverflow about it, you are acting like that the site is yours – user580950 Jul 11 '13 at 07:38
  • The questions you are asking here are of consistently rather low quality as far as my cursory check goes. You seem to be using Stack Overflow as a code writing and fixing service. That's not what the place is about. – Pekka Jul 11 '13 at 07:39
  • I agree @Pekka웃 but if the question is deleted, how is he going to read the link you posted? – icedwater Jul 11 '13 at 07:45
  • @icedwater he deleted the question himself. – Pekka Jul 11 '13 at 07:46
  • Ah, well. Can't do anything about it then :) – icedwater Jul 11 '13 at 07:46
  • 1
    btw it's `team@stackoverflow.com` @user – Pekka Jul 11 '13 at 07:47

2 Answers2

0

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
        )

)
Expedito
  • 7,771
  • 5
  • 30
  • 43
  • This is fine code, but is it really necessary to feed a [help vampire](http://meta.stackexchange.com/questions/19665/the-help-vampire-problem) on his quest to harvest data from a copyright-protected web site? [This](http://i.stack.imgur.com/Q8SyK.jpg) is what he wants to parse – Pekka Jul 11 '13 at 07:42
  • Stop hiding errors i.e. remove `@`. – Leri Jul 11 '13 at 07:43
  • @PédeLeão Sorry for not explaining I thought you could get my point. At development environment you should not hide _any_ error so you could know what's potentially buggy. – Leri Jul 11 '13 at 07:47
  • 1
    @PLB - I don't usually hide errors, and I hate code that uses @s, but when you are parsing someone else's HTML, sometimes it just makes sense. – Expedito Jul 11 '13 at 07:49
  • @Pekka웃 - He's not violating copyright law if he's not publishing the data. They are making the data publically available for people to use, and I have no reason to assume that he is going to abuse that privilege. – Expedito Jul 11 '13 at 07:54
  • Well, their T&C prohibit even that. http://www.kitco.com/TermsofUse/ `All Content supplied by Kitco, constitutes part of Kitco's confidential and proprietary information. In accessing the Websites or any of the Services, it is strictly prohibited to copy, reproduce, republish, store, retransmit, alter, modify, distribute, make public use thereof, create derivative works from, reverse engineer, disassemble or try to locate source code, of the Content, and any software or applications used by Kitco` – Pekka Jul 11 '13 at 08:02
  • @Pekka웃 - Did you notice that Kitco's robot.txt file allows robots to access their reports directory? – Expedito Jul 11 '13 at 08:47
  • @Pé your point being? – Pekka Jul 11 '13 at 08:48
  • @Pekka웃 - You're trying to prohibit something that they're not. – Expedito Jul 11 '13 at 08:49
  • Robots.txt settings do not override the Terms and Conditions, which are pretty clear. – Pekka Jul 11 '13 at 08:52
  • @Pekka웃 - Are you their lawyer? Robots don't read Terms and Conditions. They read robot.txt files. I really don't want to participate in your witch hunt or vampire hunt - whatever you want to call it. If you have a problem with them allowing access in defiance with their terms and conditions, you need to bring that up with Kitco. I'm not going to worry about it. – Expedito Jul 11 '13 at 08:56
  • Meh. If you want to spend your time answering such questions, be my guest. I just think there's infinitely more interesting and answer-deserving questions on this site. – Pekka Jul 11 '13 at 08:58
  • Pekka seems to be the lawyer on the internet – user580950 Jul 11 '13 at 11:06
0

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

Teun Pronk
  • 1,367
  • 12
  • 24