Possible Duplicate:
Simulate php array language construct or parse with regexp?
suppose I have the string
$str = "array(1,3,4),array(array(4,5,6)),'this is a comma , inside a string',array('asdf' => 'lalal')";
and I try to explode this into an array by comma so that the desired end result is
$explode[0] = array(1,3,4);
$explode[1] = array(array(4,5,6));
$explode[2] = 'this is a comma , inside a string';
$explode[3] = array('asdf' => 'lalal');
simply calling explode(',',$str) is not going to cut it since there are also commas within those chunks…
is there a way to explode this reliably even if there is commas inside the desired chunks
PHP by default does not provide such a function. However you have a compact subset of PHP inside your string and PHP offers some tools here: A PHP tokenizer and a PHP parser.
Therefore it’s possible for your string specification to create a helper function that validates the input against allowed tokens and then parse it:
The tokens used are:
The token index number can be given a token name by using
token_name.Which gives you (Demo):