views:

286

answers:

4

I have to work with strings which may contain Lat/Long data, like this:

$query = "-33.805789,151.002060";
$query = "-33.805789, 151.002060";
$query = "OVER HERE: -33.805789,151.002060";

For my purposes, the first 2 strings are correct, but the last one isn't. I am trying to figure out a match pattern which would match a lat and long separated by a comma, or a comma and a space. But if it has anything in the string other than numbers, spaces, dots, minus signs and commas, then it should fail the match.

Hope this makes sense, and TIA!

+3  A: 
^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$

The ^ at the start and $ at the end make sure that it matches the complete string, and not just a part of it.

Wim
YES, that worked perfectly, thank you!!!!
Alexia
@Alexia you can mark the answer as accepted by clicking the check mark next to it.
Pekka
Oh, sorry I never saw that before ... didn't know there was a check lol!
Alexia
That regex doesn't match *-33, 151* which are valid coordinates.
chelmertz
I know there needs to be a `.` in the current version for it to mach. If you want the `.` to be optional and match all of `30`, `30.4` and `.4`, you could use `\d*\.?\d+`.
Wim
+1  A: 

It's simplest to solve with a regex as suggested in the other answers. Here is a step-by-step approach that would work too:

$result = explode(",", $query);  // Split the string by commas
$lat = trim($result[0]);         // Clean whitespace
$lon = trim($result[1]);         // Clean whitespace

if ((is_numeric($lat)) and (is_numeric($lon))) echo "Valid coordinates!";

This solution will accept arbitrary data after a comma:

 "-33.805789,151.002060,ABNSBOFVJDPENVÜE";

will pass as ok.

As Frank Farmer correctly notes, is_numeric will also recognize scientific notation.

Pekka
Looks a bit easier than the regex... ;-)
Wim
You forgot the delimiter in `explode` though: `explode(',', $query)`
Wim
@Wim I noticed, corrected. Cheers.
Pekka
is_numeric is more permissive than you'd like for many uses. `is_numeric('1e6')` returns `bool(true)` because it recognizes it as scientific notation
Frank Farmer
Thanks, but that wouldn't work for me, because the string might not even have comma in it, it might be only text.
Alexia
@Alexia I see, I overread that. Your requirements could be put into that code but a regex would do that more elegantly, I think you should go with a regex in this case.
Pekka
A: 
/^-*\d*\.\d+,[\b]*-*\d*\.\d+$/
stillstanding
A: 

The regex approach can't really validate that longitude and latitude are valid, but here's one that would be more precise than the others posted already:

/^\s*-?\d{1,3}\.\d+,\s*\d{1,3}\.\d+\s*$/

This would reject some strings that others' solutions would allow, such as

-1-23-1-,210-
--123.1234,123.1234

But it would still allow invalid values like this:

361.1234, 123.1234

Your best bet -- if you need serious validation -- is to create a class to store and validate these coordinates.

mehaase
Thx! I will try this one also ... The reason I need this is to just check for a valid "looking" lat/long. I then send them to an actual geocoder and it actually validates it for a real location. Just want to limit the amount of data I send to the geocoder.
Alexia