Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

preg_match() on a meta field value WordPress


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.


Answers (2)


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

[[LINK href=""]][[/LINK]]
[[LINK href=""]][[/LINK]]
[[LINK href=""]][[/LINK]]

designbuildtest comments:

Works great. Thanks Martin.


Arnav Joy answers:

try this

$custom_field_name = "latitude"; // change here

$latVal = get_post_meta(get_the_ID(),$custom_field_name, true);

$latVal = explode(',',$latVal);

echo 'first Value=='.$latVal[0]."<br>";
echo 'second Value=='.$latVal[1]."<br>";


designbuildtest comments:

Thanks Arnav. This works great also.