I've tried about a million different regular expressions, and I just can't wrap my head around this (admittedly, a lot of regular expressions from my understanding).
In my text, I have the following variables:
{{$one}} {{$three.four.five}} {{$six.seven}}
And I have an array with all the replacements for them (index for "one" - "one", etc.), but some may be missing.
I want to replace from an array if it exists, and if you don't leave the text alone.
What can I use regex for preg_match_all variables in the text text in the fragment below, replace from $ replace where necessary, and echo in 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); }
http://codepad.viper-7.com/4INvEE
It:
'/\{\{\$(\w+)\}\}/e'
as in the fragment, is the closest I received.
It should also work with the name do in variable names.
Thanks in advance for your help!
php regex replace search preg-match
Alex howe
source share