這應做到:
$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