Question

I have this page which I am using to post (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>

and this is my page:

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

    }

?>

When I use $match_rota1[1]; AND $match_rota2[1]; it works properly, showing the result B763 B763 B763 A332

But when I try to use $match_rota1[2]; AND $match_rota2[2]; I get a blank page instead of the results, like UM409 EKUBA UM409 DADOT UL304 ISANI UL304

-EDIT-

From each line like this

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

I expect this UM409 EKUBA UM409 DADOT UM409 VUMPI UM409 instead of this B763

Why is my code not working??

Was it helpful?

Solution 2

Problem solved, sorry:

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] 

    }

OTHER TIPS

I see only one set of matching parens, so I'm not sure why there would be a second match. Where are you expecting $match_rota2[2]; to come from?

You can try this:

$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]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top