I will have an input like such:
equals(plus(two,three),five)
I make no assumptions about how far nested the functions could be, or what their names or argument lists will be. My goal is to find all of the arguments of the outermost functions, and then evaluate each of the next levels recursively.
So, what I did first was:
$piece = 'equals(plus(two,three),five)';
preg_match( '/^(?:(' . $constant . ')\\((.+)\\))$/mx', $piece, $matches );
And this yields:
Array
(
[0] => equals(plus(two,three),five)
[1] => equals
[2] => plus(two,three),five
)
Now I have the name of the outermost function in $matches[1], and now I need to split $matches[2], a comma-separated list, to evaluate each item recursively.
I can't just split it by comma, though, because there is another comma inside the plus function, which is an argument. So my list items are not "plus(two", "three)", and "five", but rather "plus(two,three)" and "five".
I tried lookahead and lookbehind, but of course I get the error that my lookahead and lookbehind are not of fixed length.
// split by commas that are NOT inside any brackets
$args = preg_split( '/(?<!(\\(.*)))\\,(?!(.*\\))/mx', $matches[2] );
How can I split these up?! Any ideas, or improvements, or completely alternate suggestions?
Thanks,
Jared