Avatar of Doomish
Doomish

asked on 

RegExp to extrct longitude and latitude from a string

I inherited a database with a location field that contains data in the following style:

a:3:{s:7:"address";s:32:"Belgrade, Serbia";s:3:"lat";s:10:"44.8234945";s:3:"lng";s:17:"20.46218669999996";}
a:3:{s:7:"address";s:30:"Los Angeles, CA, United States";s:3:"lat";s:10:"34.0522342";s:3:"lng";s:12:"-118.2436849";}
a:3:{s:7:"address";s:58:"Wauwatosa, United States";s:3:"lat";d:43.05008220000001;s:3:"lng";d:-87.9901927;}
a:3:{s:7:"address";s:32:"London, England";s:3:"lat";d:51.4743113;s:3:"lng";d:-0.1010046;}

The data in the field varies, but what is constant in all of them is the last part of the string: "lat";d:51.4743113;s:3:"lng";d:-0.1010046;}
I would like to clean them up and extract only the lat and lng from the string from the above and echo them like:

Coordinates: 51.4743113,-0.1010046

Any help is appreciated, many thanks!



Regular ExpressionsPHP

Avatar of undefined
Last Comment
Kimputer
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Doomish
Doomish

ASKER

Thank you for the help!
I will loop through the table and check for inconsistencies, and discard if they were tampered with.


Avatar of Kimputer
Kimputer

If serialization doesn't help you, then just use this (https://regex101.com/:

[-]{0,1}\d{1,3}.\d{1,20}

Open in new window




User generated image
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo