Skip to content

Instantly share code, notes, and snippets.

@DamChtlv
Created March 9, 2023 18:05
Show Gist options
  • Save DamChtlv/1ff3c8924aa093c90d3f16c84aa837d2 to your computer and use it in GitHub Desktop.
Save DamChtlv/1ff3c8924aa093c90d3f16c84aa837d2 to your computer and use it in GitHub Desktop.
Latitude Longitude Regex PHP
<?php
// Get Lat / Long coordinates as separate values
$latlong = array();
$str = '-28.0166667, 153.4';
preg_match_all( '/[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)|(90(\.0+)?)/', $str, $matches );
if ( $matches && is_array( $matches ) ) {
$matches = reset( $matches );
// Group values by 2 (sometimes we have more than 1 latlong in a cell)
$latlong = is_array( $matches ) ? array_chunk( $matches, 2 ) : $matches;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment