0

I've been searching through previous questions for the past hour, so if this exists somewhere else, sorry, but I didn't find it. I'm pretty new to PHP, so forgive me if it's a newbie question. I searched through Google, too.

I'm trying to automate getting the lat/long information from airnav.com for various airports for map updates. I've got an XML file with the airport codes:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AIRPORTS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <AIRPORT>MXA</AIRPORT>
  <AIRPORT>SDM</AIRPORT>
</AIRPORTS>

My PHP is:

<?php
  $xml=simplexml_load_file("airports.xml");

  foreach($xml->AIRPORT as $arpt)
  {
    if (preg_match('#[0-9]#', $arpt)){
      $arptName = $arpt;}

    else{
      $arptName = "K" . $arpt;}

    $content = file_get_contents("http://www.airnav.com/airport/" . $arptName);

    preg_match('#([0-9]{2,2}\.[0-9]{1,})\s\/\s(\-[0-9]{2,2}\.[0-9]{1,})#', $content, $arptmatch);

    $lat = $arptmatch[1]; //ERROR HERE (line 27)
    $long = $arptmatch[2]; //AND HERE (line 28)

    echo "Airport: $arpt\nLat: $lat\nLong: $long\n";
  }
?>

This works just fine for the first airport, I get this:

Airport: MXA Lat: 35.8944444 Long: -90.1545833 

Which, when I go to the site and check is correct. But for the second airport, I get:

Airport: SDM Lat: Long: 

Along with the following errors:

Notice: Undefined offset: 1 in /home/a5308473/public_html/phpStuff/readFromAirNav.html on line 27
Notice: Undefined offset: 2 in /home/a5308473/public_html/phpStuff/readFromAirNav.html on line 28

I can't seem to find any way to fix this. Anyone know what's wrong?

  • Maybe your `preg_match()` is not finding a match? You should check return results of `preg_match()` before trying to operate against them. So something like `$result = preg_match(...); if (1 === $result) { // you have a match $lat = ...; $long = ...; }` – Mike Brant Oct 21 '13 at 21:38
  • I just want to point out that your RE will only match on negative longitudes – Mike Stotts Oct 21 '13 at 21:54

2 Answers2

1

You're simply assuming that preg_match did find something, and blindly barge on ahead, even if no matches were actually made.

Try

if (preg_match(...)) {
    $lat = $arptmatch[1];
    $long = $arptmatch[2];
} else {
    $lat = 'n/a';
    $long = 'n/a';
}

instead.

Marc B
  • 356,200
  • 43
  • 426
  • 500
0

It's because the coordinates for SDM are 32.5722722 / -116.9801611. In the second part of your regular expression, 116 must be between 2 and 2 characters {2,2}. Change it so that it matches 2 to 3 characters, {2,3}. If you are only using United States coordinates then {2,3} will work, but outside of the United States you could run into coordinates that have a single digit integer-part. {1,3} would be match the integer-part of any longitude and {1,2} would be a match for the integer-part of any latitude.

Your regular expression should be

([0-9]{1,2}\.[0-9]{1,})\s\/\s(\-[0-9]{1,3}\.[0-9]{1,})

Because your regular expression doesn't match anything, the results are a 0 length array and your index offset is greater than the length of the array.

Additionally, {2,2} can be written as {2} and {1,} can be written as +

Nathanial
  • 2,177
  • 1
  • 15
  • 34
  • Yep, that's it. I guess I'd been looking at the code for too long. Thank you! – Rachel Vega Oct 21 '13 at 23:31
  • Yes, there are some tricks for effective troubleshooting, for example using a step-debugger and looking just before the error comes if the variables contain what they have (because you can inspect the program when it runs). – hakre Oct 23 '13 at 15:07