In a meta field I have a geocode stored in the format: -33.873651,151.20689
How do I create a preg_match() statement (or something else) in PHP to extract the individual latitude and longitude values? I'm guessing the code would first have to look for and extract the value found before the comma and then extract the value found after the comma.
Thanks
Martin Pham answers:
try this
$geocode = '-33.873651,151.20689';
$code = explode(',' , $geocode);
echo $code[0]; //-33.873651
echo $code[1]; //151.20689
Martin Pham comments:
you can use (Deprecated as of PHP 5.3.0)
$geocode = '-33.873651,151.20689';
//list($latitude, $longitude) = explode(',' , $geocode);
list($latitude, $longitude) = split(',', $geocode);
echo $latitude.'<br/>'; //-33.873651
echo $longitude; //151.20689
More:
[[LINK href="http://php.net/manual/en/function.split.php"]]http://php.net/manual/en/function.split.php[[/LINK]]
[[LINK href="http://php.net/manual/en/function.list.php"]]http://php.net/manual/en/function.list.php[[/LINK]]
[[LINK href="http://php.net/manual/en/function.explode.php"]]http://php.net/manual/en/function.explode.php[[/LINK]]
designbuildtest comments:
Works great. Thanks Martin.
Arnav Joy answers:
try this
<?php
$custom_field_name = "latitude"; // change here
$latVal = get_post_meta(get_the_ID(),$custom_field_name, true);
if(!empty($latVal)){
$latVal = explode(',',$latVal);
echo 'first Value=='.$latVal[0]."<br>";
echo 'second Value=='.$latVal[1]."<br>";
}
?>
designbuildtest comments:
Thanks Arnav. This works great also.