You'll be spoiled for choice!
This is another answer, if you want extract multiple substrings with name, age, city.
This is simple solution, using substr
, explode
and str_replace
:
$array = array();
foreach( explode( ']}{[', substr( $string,2,-2 )) as $chunk )
{
$array[] = str_replace( '][', ',', $chunk );
}
print_r( $array );
eval.in demo
Obviously, it works only if there are not curly or squared brackets inside the sigle strings.
First of all, it remove from the original string opening and closing brackets, then explode
(transform a string in array) the string by ][
and performs a foreach
loop through obtained elements (John][16 years old][New York
etc...); for each elements it replace every occurrence of ][
with ,
and append it to desired array.
That's all