Search code examples
phpregexstrpos

Find if a certain value exists in a string


All, I'm using the geocoding API from Google. If someone enters St. Louis for example it returns a city of St Louis.

My issue is that in my database this is listed at Saint Louis so it doesn't find an entry. Do I want to use a regular expression or something like that to determine if the the first couple characters are something like St ?

If it finds the St in the string then I'd like to replace it with Saint. I think it only has to be at the beginning because something like State College I wouldn't want it replaced.

Any help you could provide would be greatly appreciated! Thanks!


Solution

  • I took the suggestion of ceejayoz and took the lat/long results returned by the geocode API in this string:

    http://maps.googleapis.com/maps/api/geocode/json?address=st.%20louis%20mo&sensor=true

    Then I changed my request to actually look up the lat/long by using this string:

    http://maps.googleapis.com/maps/api/geocode/json?latlng=38.6270025,-90.1994042&sensor=true

    I was then able to parse the json and obtain a zip code to use which I had stored in my database.