如何在每个之后拆分字符串]

时间:2013-11-20 17:59:49

标签: php

我有一个像格式一样的数组字符串。

["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]

我试图在每次之后拆分代码] - []内的数据每次都会变化,所以我不能使用str_split();

我想保留所有括号,以便它们不会切断所以我不能使用爆炸

谢谢

3 个答案:

答案 0 :(得分:2)

简易正则表达式:

$s = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
preg_match_all('/\[[^\]]+\]/', $s, $m);
print_r($m[0]);

但实际上它几乎是json,所以:

$s = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
$s = '[' . str_replace('] [', '],[', $s) . ']';
print_r(json_decode($s));

也许你有一个片段或修改过的json,所以如果你有真正的json可能会更容易。

答案 1 :(得分:0)

$str = '["1","Data","time"] ["2","Data","time"] ["3","Data","time"] ["4","Data","time"]';
$newStr = trim($str,'[');
$newStr1 = trim($newStr,']');
$arr = explode('] [',$newStr1);
print_r($arr);

答案 2 :(得分:-1)

最简单的解决方案(如果你不想要正则表达式)将使用两边的trim 1 char然后通过'] ['爆炸。

示例:

$string = "[123][456][789]";
$string = substr($string,1,strlen($string)-1);

$array = explode('][',$string);