这应做到:
$string = '[23,64.2],[25.2,59.8],[25.6,60],[24,51.2],[24,65.2],[3.4,63.4]';
if (preg_match_all('/,?\[([^\]]+)\]/', $string, $matches)) {
print_r($matches[1]);
}
它打印:
[0] => string(7) "23,64.2"
[1] => string(9) "25.2,59.8"
[2] => string(7) "25.6,60"
[3] => string(7) "24,51.2"
[4] => string(7) "24,65.2"
[5] => string(8) "3.4,63.4"
正则表达式的分项数字:
,? // zero or one comma
\[ // opening bracket
([^\]]+) // capture one or more chars until closing bracket
\] // closing bracket
要得到x,y坐标,然后你可以:
$coords = array();
foreach ($matches[1] as $match) {
list($x, y) = explode(',', $match);
$coords[] = array(
'x' => (float)$x,
'y' => (float)$y
);
}
非常感谢!刚才意识到我还没有理解这个数组是二维的。回显$匹配时,我不断收到错误“数组”[1]。 $ matches [1] [0]效果更好,现在只需构建循环。 再次感谢!问题已修复 – Marco 2011-03-17 17:57:45