Reputation: 9476
I have string like following format:
$string ='[(id,name,top,left,width,height,depth)filename|filename][(id,name,top,left,width,height,depth)filename|filename]';
I want to extract string for square and simple bracket and result set would be like
[0] =>(id,name,top,left,width,height,depth)
[1] => filename
[2] => filename
How can i do it? I have tried below code:
explode("[" , rtrim($string, "]"));
But not working properly.
Upvotes: 0
Views: 1042
Reputation: 459
You can use regex for this,
$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);
output
matches[1][0] => id,name,top,left,width,height,depth
matches[1][1] => filename|filename
See regex
Upvotes: 2
Reputation: 2071
Use this code
$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);
Upvotes: 0