preg_match echo无法正常工作

时间:2014-01-19 18:35:35

标签: php preg-match preg-match-all

我有这个页面,我用它来发布(rotas.php):

<tr><td>SBGL </td><td>KMIA </td><td>B763 </td><td>UM409 EKUBA UM409 DADOT UM409 VUMPI UM409 </td><td>959 </td><td>N0460 </td><td>310 </td><td>H </td><td>EET/SBBS0030 SBAZ0230 SVZM0425 </td></tr><tr><td class='alt'>SBGL </td><td class='alt'>KMIA </td><td class='alt'>B763 </td><td class='alt'>UM409 EKUBA UM409 DADOT UL304 ISANI UL304 </td><td class='alt'>727 </td><td class='alt'>N0490 </td><td class='alt'>360 </td><td class='alt'>H </td><td class='alt'>EET/SBBS0018 SBAZ0157 SVZM0400 </td></tr><tr><td>SBGL </td><td>KMIA </td><td>B763 </td><td>UM409 EKUBA UM409 DADOT UL304 ISANI </td><td>837 </td><td>N0470 </td><td>320 </td><td>H </td><td>EET/SBBS0030 SBAZ0230 SVZM0425 </td></tr><tr><td class='alt'>SBGL </td><td class='alt'>KMIA </td><td class='alt'>A332 </td><td class='alt'>UM409 EKUBA UM409 DADOT UM409 VUMPI UM409 </td><td class='alt'>849 </td><td class='alt'>N0480 </td><td class='alt'>360 </td><td class='alt'>H </td><td class='alt'>EET/SBBS0025 SBAZ0222 SVZM0442 </td></tr><tr><td colspan='9' align='center' class='alt'>4 registro(s) encontrado(s)</td></tr>

这是我的页面:

<?php

$adep = $_GET['adep'];
$ades = $_GET['ades'];


$postdata = http_build_query(
    array(
        'adep' => $adep,
        'ades' => $ades
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://www.cgna.gov.br/sistemas/harpia/rotas.php', false, $context);

preg_match_all("'<tr><td>".$adep." </td><td>".$ades." </td>(.*?)<\/tr>'si", $result, $match1);
preg_match_all("'<tr><td class=\'alt\'>".$adep." </td><td class=\'alt\'>".$ades." </td>(.*?)<\/tr>'si", $result, $match2);

foreach($match1[1] as $rota)

    {
         preg_match("'<td>(.*?)<\/td>'si", $rota, $match_rota1);
         echo $match_rota1[1];


    }

    foreach($match2[1] as $rota)

    {
         preg_match("'<td class=\'alt\'>(.*?)<\/td>'si", $rota, $match_rota2);
         echo $match_rota2[1];

    }

?>

当我使用$match_rota1[1]; AND $match_rota2[1];时,它正常工作,显示结果B763 B763 B763 A332

但是当我尝试使用$match_rota1[2]; AND $match_rota2[2];时,我会得到一个空白页而不是结果,例如UM409 EKUBA UM409 DADOT UL304 ISANI UL304

- 编辑 -

从这样的每一行

<tr><td>SBGL </td><td>KMIA </td><td>B763 </td><td>UM409 EKUBA UM409 DADOT UM409 VUMPI UM409 </td><td>959 </td><td>N0460 </td><td>310 </td><td>H </td><td>EET/SBBS0030 SBAZ0230 SVZM0425 </td></tr>

我希望这个UM409 EKUBA UM409 DADOT UM409 VUMPI UM409代替此B763

为什么我的代码无效?

3 个答案:

答案 0 :(得分:0)

我只看到一组匹配的parens,所以我不确定为什么会有第二场比赛。你期待$ match_rota2 [2];从......来?

答案 1 :(得分:0)

问题解决了,抱歉:

foreach($match2[1] as $rota)

    {
         preg_match_all("'<td class=\'alt\'>(.*?)<\/td>'si", $rota, $match_rota2);  // preg_match_all instead of preg_match
         echo $match_rota2[1][1]."<br>"; // added [1] 

    }

答案 2 :(得分:-1)

你可以试试这个:

$result = file_get_contents('http://www.cgna.gov.br/sistemas/harpia/rotas.php', false, $context);

$pattern = <<<LOD
~
(?(DEFINE) (?<td> <td (?: \s+ class='alt')?> )) # define the opening tag subpattern
<tr>
  \g<td> $adep \s* </td>
  \g<td> $ades \s* </td>
  \g<td> [^<]* </td> \g<td> \K                  # the \K reset all the match before
  [^<]+
  (?= \s+ < )
~xi
LOD;

preg_match_all($pattern, $result, $matches);
print_r($matches[0]);