phpregexpreg-replacetext-extraction

Get comma-separed latitude and longitude values from a larger body of text


I have a string like the one below:

".......mapsearch"), '52.486683, -4.044363', options......"

I want to retrieve the 2 numbers (long & lat) from the string - I don't mind if I retrieve them together as a single string complete with the comma and space in between.

I know that I want to use an expression to match and ignore anything from the start up to and including the mapsearch"), ' and the same for the end of the string to the ', options pattern.

UPDATE

The string will also contain a range of other characters like { } ( ) = : / and numbers before and after the desired match..


Solution

  • This will work, as ugly as it is:

    /mapsearch\"\), '(-?\d+(?:\.\d+)?), (-?\d+(?:\.\d+)?)', options/
    

    The numbers can be negative or positive, and also it will match integers and decimal values. The pattern will return both numbers as a separate group.