Page 1 of 1

preg_martch_all for {$varname}

Posted: Tue Sep 26, 2006 5:27 am
by CoderGoblin
OK I think I am being stupid..

I need to pick up all '{$varname}' in a piece of text where var name can also be arrays.

I have tried multiple patterns with the first being

Code: Select all

preg_match_all('/(\$\{.+\})/simu',$text,$matches);
but it never finds the information. What am I doing wrong ?

Posted: Tue Sep 26, 2006 5:38 am
by volka
preg_match_all('/(\$\{.+\})/simu',$text,$matches);
this matches ${varname} but not {$varname}.
And you should use the modifier U (ungreedy).

Posted: Tue Sep 26, 2006 5:44 am
by CoderGoblin
Thanks volka, unfortunately it didn't work as I need the {$varname}...

This however did...

Code: Select all

preg_match_all('((?!{)\$[^}]+)',$text,$matches);
It may have been the /simu not being needed but I am unsure.. Will test more later..

Posted: Tue Sep 26, 2006 5:47 am
by volka
No, the reason is
volka wrote:
preg_match_all('/(\$\{.+\})/simu',$text,$matches);
this matches ${varname} but not {$varname}.