0

使用以下字符串:

$str = '["one","two"],a,["three","four"],a,,a,["five","six"]';

preg_split( delimiter pattern, $str );

我将如何设置分隔符模式才能获得此结果:

$arr[0] = '["one","two"]';
$arr[1] = '["three","four"]';
$arr[2] = '["five","six"]';

换句话说,有没有办法在模式 ',a,' AND ',a,,a,' 但首先检查 ',a,,a,' 进行拆分,因为 ',a,' 是',一个,,一个,'?

提前致谢!

4

4 回答 4

1

看起来您实际上想要做的是将方括号中的部分分开。你可以这样做:

$arr = preg_split("/(?<=\])[^[]*(?=\[)/",$str);
于 2013-03-07T22:04:19.420 回答
1

如果它只能是,a,and ,a,,a,,那么这就足够了:

preg_split("/(,a,)+/", $str);
于 2013-03-07T22:07:27.813 回答
0

如果您只想要括号之间的内容,我认为您应该使用 preg_match 而不是 preg_split

  1. 使用正则表达式提取括号中的内容
  2. php preg_split() 查找两个单词之间的文本
于 2013-03-07T22:03:38.593 回答
0

看看这段代码:

$result = array();

preg_match_all("/(\[[^\]]*\])/", '["one","two"],a,["three","four"],a,,a,["five","six"]', $result);

echo '<pre>' . print_r($result, true);

它将返回:

Array
(
    [0] => Array
        (
            [0] => ["one","two"]
            [1] => ["three","four"]
            [2] => ["five","six"]
        )

    [1] => Array
        (
            [0] => ["one","two"]
            [1] => ["three","four"]
            [2] => ["five","six"]
        )
)
于 2013-03-07T22:03:58.333 回答