I have string
$argsInString = '"%s hello", \'%s world\', $foo->bar("anything", array(\'foo\' => 5 , \'bar\' => $a)),5, foo($s) ,$foo';
And I want convert to array like this:
$argsInArray = array('"%s hello"', '\'%s world\'', '$foo->bar("anything", array(\'foo\' => 5 , \'bar\' => $a))', '5', 'foo($s)', '$foo');
I try anything like this:
eval('$argsInArray = array(' . $argsInString . ');');
but this execute variables.
Could you help me how create $argsInArray from $argsInString?
EDIT:
If it succeed to write a regular expression that would clothed individual parameters, using a single quote (‘), in the string, then the eval work as described above.
Or could you write regular expression preg_match_all(‘…’, $argsInString, $argsInArray); this is best.
Here’s a Regex that will work on samples like this:
Explained demo here: http://regex101.com/r/hK6nN0
Update:
to also match single and double quoted strings that contain
,like:"%s, hello", \'%s, world\', 5/(?: ?)([^,]*\(.*?\)|[^,]*'[^']*'|[^,]*"[^"]*"|.+?)(?: ?)(?:,|$)/Explained demo here: http://regex101.com/r/tD3cN9