PHP preg_split分隔符模式,在字符链上分割


PHP preg_split delimiter pattern, split at character chain

使用以下字符串:

$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,' BUT检查',a,,a,'首先因为',a,'是',a,,a,'的子字符串?

提前感谢!

如果只能是,a,,a,,a,,那么这应该足够了:

preg_split("/(,a,)+/", $str);

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

$arr = preg_split("/(?<='])[^[]*(?='[)/",$str);

看一下这段代码:

$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"]
        )
)