I have tried about a million different regexes and I just can't wrap my head around this one (admittedly a lot of regex is out of my grasp).
In my text I have variables like this:
{{$one}}
{{$three.four.five}}
{{$six.seven}}
And I have an array with all the replaces for them (the index for 'one' is 'one' etc) but some may be missing.
I want to replace from the array if it exists and if not leave the text alone.
What regex can I use to preg_match_all of the variables in $text in the snippet below, replace from $replace where appropriate and echo out to the browser?
<?php
$replaces = array('testa.testb.testc' => '1', 'testc.testa' => '2', 'testf' => '3');
$text = '{{$testa.testb.testc}}<br>{{$testc.testa}}<br>{{$testf}}<br>{{$aaaaa}}<br>';
preg_match_all('/\{\{\$(\w+)\}\}/e', $text, $matches);
foreach($matches as $match)
{
$key = str_replace('{{$', '', $match);
$key = str_replace('}}', '', $key);
if(isset($replaces[$key]))
$text = str_replace($match, $replaces[$key], $text);
}
// I want to end up echo'ing:
// 1<br>2<br>3<br>{{$aaaaa}}<br>
echo $text;
?>
http://codepad.viper-7.com/4INvEE
This:
'/\{\{\$(\w+)\}\}/e'
like in the snippet, is the closest I have gotten.
It has to work with the does in the variable names too.
Thanks in advance for all the help!