拆分字符串在 PHP 中包含方括号

Split String contain Brackets in PHP

我有如下格式的字符串:

$string ='[(id,name,top,left,width,height,depth)filename|filename][(id,name,top,left,width,height,depth)filename|filename]';

我想提取方括号和简单括号的字符串,结果集就像

[0] =>(id,name,top,left,width,height,depth)
[1] => filename
[2] => filename

我该怎么做?我试过下面的代码:

explode("[" , rtrim($string, "]"));    

但无法正常工作。

您可以为此使用正则表达式,

$re = "/\[\((.*?)\)(.*?)\]/s"; 
$str = "[(id,name,top,left,width,height,depth)filename|filename][(id,name,top,left,width,height,depth)filename|filename]'"; 

preg_match_all($re, $str, $matches);

输出

matches[1][0] => id,name,top,left,width,height,depth
matches[1][1] => filename|filename

regex

使用此代码

$string ='[(id,name,top,left,width,height,depth)filename|filename][(id,name,top,left,width,height,depth)filename|filename]';
$array = explode('][',$string);
$array = array_unique(array_filter($array));
$singleArr = array();
$singleArr[] = str_replace('[','',$array['0']);
$singleArr[] = str_replace(']','',$array['1']);

$singleArr = array_unique($singleArr);
//print_r($singleArr);

$blankArr = array();
foreach($singleArr as $val)
{
    $first = explode('|',$val);
    $blankArr['0'] = substr($first['0'],0,-8);
    $blankArr['1'] = substr($first['0'],-8);
    $blankArr['2'] = $first['1'];
}
print_r($blankArr);