PHP (among others) will execute the deepest function first, working its way out. For example,
$text = strtoupper(str_replace('_', ' ', file_get_contents('file.txt')));
I’m doing something very similar to the above example for a template parser. It looks for the tags
{@tag_name}
and replaces it with a variable of the name $tag_name. One more example:
$a = 'hello'; $b = ' world'; INPUT = 'This is my test {@a{@b}} string.'; OUTPUT (step 1) = 'This is my test {@a} world string.'; OUTPUT output = 'This is my test hello world string.';
How can I go about doing this? Does this make sense? If not, I can try explaining better.
I’m not sure I understand the nesting in your example, as the example doesn’t demonstrate a purpose behind nesting. Your example input could very easily be
And using arrays in str_replace would handle this very simply and quickly.
Which gives us
Now, a recursive function for this isn’t too difficult, though I’m not sure I understand how useful it would be. Here’s a working example:
That print_r will show you what the matches look like so you can follow the function through its paces. Now lets try it out…
First Result:
Now lets try String Two
Second Result:
That may very well be what you’re looking for. Obviously you would need an
aworldvariable for this to work recursively…And our result.
And just for some fun with the recursion…
Not sure if this is what you’re asking for…