0

I have html of a webpage in a php string, that has, among other things, a table. The table has many rows, out of which I want to extract information about two rows, class.day and class.odd. Each of them are getting repeated multiple times.

I want to have the information in an array out of this, something like (or maybe in an associative array) :

$array1 = array(17=>'', 18=>'', 19=>150, 20=>145, 21=>175)
$array2 = array(17=>'', 18=>'', 19=>90, 20=>75, 21=>120)
$array3 = array(...)
$array4 = array(...)

I was wondering how can I achieve that, can anyone help with any suggestion ?

Thanks a lot.

<table class="matrix">
    <tr ...></tr>
    <tr ...></tr>
    <tr class="day">
        <td class="fill_row" colspan="2"></td>
        <td class="past">17</td>
        <td class="past">18</td>
        <td>19</td>
        <td>20</td>
        <td>21</td>
    </tr>
    <tr ...></tr>
    <tr ...></tr>
    <tr class="odd">
        <td class="fill_row" colspan="2"></td>
        <td class="past"> </td>
        <td class="past"> </td>
        <td>150</td>
        <td>145</td>
        <td>175</td>
    </tr>
    <tr ...></tr>
    <tr ...></tr>
    <tr class="day">
        <td class="fill_row" colspan="2"></td>
        <td class="past">17</td>
        <td class="past">18</td>
        <td>19</td>
        <td>20</td>
        <td>21</td>
    </tr>
    <tr ...></tr>
    <tr ...></tr>
    <tr class="odd">
        <td class="fill_row" colspan="2"></td>
        <td class="past"> </td>
        <td class="past"> </td>
        <td>90</td>
        <td>75</td>
        <td>120</td>
    </tr>
</table>
Jeremy
  • 975
  • 4
  • 15
  • 26
  • http://simplehtmldom.sourceforge.net – tenub Feb 19 '14 at 17:56
  • thanks guys, but simplehtmldom is about fetching information from a webpage, here I have the information already in a string, and I need to extrach specific tr classes from it ... – Jeremy Feb 21 '14 at 09:51

1 Answers1

1

its not perfect but it works ^^

<?php
$strRegEx = '#<tr class="(day|odd)">.{1,10}<td .{28}>([0-9]*)</td>.{1,10}<td .{12}>(.{1,5})</td>.{1,10}<td .{12}>(.{1,5})</td>.{1,10}<td>([0-9]*)</td>.{1,10}<td>([0-9]*)</td>.{1,10}<td>([0-9]*)</td>.{1,10}</tr>#s';
$regEx = preg_match_all($strRegEx , $strYourHtml, $arrTable);

if ($regEx) {
    $arrResults = array();
    foreach($arrTable[1] as $strKey => $arrResult){
        $arrResults[$strKey]["name"] = $arrResult;
        $arrResults[$strKey]["value_1"] = $arrTable[2][$strKey];
        $arrResults[$strKey]["value_2"] = $arrTable[3][$strKey];
        $arrResults[$strKey]["value_3"] = $arrTable[4][$strKey];
        $arrResults[$strKey]["value_4"] = $arrTable[5][$strKey];
        $arrResults[$strKey]["value_5"] = $arrTable[6][$strKey];
        $arrResults[$strKey]["value_6"] = $arrTable[7][$strKey];
    }
} else {
    $arrResults = false;
}

print_r($arrResults);
?>
  • Hi, thanks for this. So here $strYourHtml will be my string that holds the whole html page, but what is $arrTable please? – Jeremy Feb 21 '14 at 09:35
  • Oh I just found that in this example we have assumed that the number of td will remain constant. I forgot to mention that the number of td in my case is variable. Probably we need a different logic in that case ? – Jeremy Feb 21 '14 at 09:49