PHP - Parse template variable with regex

swat2008

New Member
I have to parse this template file ($html) :\[code\]{$myFirstVariable}{$myMainVar:MYF1,"x\:x\,x",2:MYF2:MYF3,false}{$myLastVariable:trim}\[/code\]Following, my php parser :\[code\]$regexp = '#{\$(?<name>.+?)(\:(?<modifiers>.+?))?}#';preg_replace_callback($regexp, 'separateVariable', $html);function separateVariable($matches) { $varname = $matches['name']; print $varname."\n"; if (isset($matches['modifiers'])) { $modifiers = $matches['modifiers']; $modifiers = preg_split('#(?<!\\\):#', $modifiers); $parsed = array(); foreach ($modifiers as $modifier) { $modifier = preg_split('#(?<!\\\),#', $modifier); $parsed[array_shift($modifier)] = $modifier; } // parsed[myFuncName] = Array(2ndArg, 3rdArg) print_r($parsed); } print "\n";}\[/code\]All working except i've to escape ':' and ',' in {$myMainVar:...} with an '\'.Do you have any solution to free me up of '\' ?Thanks.
 
Back
Top