views:

48

answers:

2

on search i want to check if query is latitude and longitude or just text search with php.

$query = '-122.0307642, 37.3316930';

latitude and longitude can have '-' in them also.

what preg_match pattern it will be for this?

A: 

Something like this:

(-?\d+(?:\.\d+)?)(?:,\s*|\s+)(-?\d+(?:\.\d+)?)

This will net you two matches, which you can then cast to floats and check against lat/lng bounds.

This will also allow the two numbers to be separated by space. If you want to only allow a comma, use:

(-?\d+(?:\.\d+)?),\s*(-?\d+(?:\.\d+)?)
Max Shawabkeh
is this good? i added space before the comma too (-?\d+(?:\.\d+)?)(\s*|\s+)(?:,\s*|\s+)(-?\d+(?:\.\d+)
Basit
A: 

The following regex will match two numbers (with or without a negative sign, separated by a comma) of the format a.b where a and b are digit-sequences of minimum length one.

(-?\d+\.\d+)\s*,\s*(-?\d+\.\d+)
Amarghosh