Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用regexp从表中获取td数据时,表位于变量中_Php_Regex - Fatal编程技术网

Php 使用regexp从表中获取td数据时,表位于变量中

Php 使用regexp从表中获取td数据时,表位于变量中,php,regex,Php,Regex,我有一个php字符串中的网页html,其中包括一个表。这个表有很多行,我想从中提取关于两行的信息,class.day和class.odd。每一个都被重复多次 我想把这些信息放在一个数组中,比如(或者放在一个关联数组中): 我想知道我怎样才能做到这一点,有人能提供一些建议吗 非常感谢 <table class="matrix"> <tr ...></tr> <tr ...></tr> <tr class="d

我有一个php字符串中的网页html,其中包括一个表。这个表有很多行,我想从中提取关于两行的信息,class.day和class.odd。每一个都被重复多次

我想把这些信息放在一个数组中,比如(或者放在一个关联数组中):

我想知道我怎样才能做到这一点,有人能提供一些建议吗

非常感谢

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

17
18
19
20
21
150
145
175
17
18
19
20
21
90
75
120

虽然它并不完美,但很有效^^

<?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);
?>


谢谢大家,但是simplehtmldom是关于从网页获取信息的,这里我已经有了字符串中的信息,我需要从中提取特定的tr类……嗨,谢谢。所以这里$strYourHtml将是我的字符串,它保存整个html页面,但是$arrTable是什么呢?哦,我刚刚发现,在这个例子中,我们假设td的数量将保持不变。我忘了提到,在我的案例中,td的数量是可变的。在这种情况下,我们可能需要不同的逻辑?
<?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);
?>