在PHP中将字符串操作分为两部分
问题描述:
Can someone suggest a simple function that will parse a string into two parts, based on everything before and after the comma? I want to split up a latitude longitude pair so that I have two variables instead of one.
I need to turn this:
-79.5310706,43.6918950
into this:
-79.531070 43.6918950
有人可以建议一个简单的函数,根据逗号前后的所有内容将字符串解析为两部分吗? 我想拆分一个纬度经度对,这样我就有两个变量而不是一个。 p>
我需要转一下: p>
-79.5310706,43.6918950 p> blockquote>进入: p>
-79.531070 43.6918950 p > blockquote> div>
答
$parts = explode(",", "-79.5310706,43.6918950");
echo $parts[0];
// -79.5310706
echo $parts[1];
// 43.69189506
Or if you need it to stay as a space-separated string, just str_replace()
the comma to a space!
echo str_replace(",", " ", "-79.5310706,43.6918950");
// -79.531070 43.6918950