preg_martch_all for {$varname}

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

preg_martch_all for {$varname}

Post 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 ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

preg_match_all('/(\$\{.+\})/simu',$text,$matches);
this matches ${varname} but not {$varname}.
And you should use the modifier U (ungreedy).
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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..
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No, the reason is
volka wrote:
preg_match_all('/(\$\{.+\})/simu',$text,$matches);
this matches ${varname} but not {$varname}.
Post Reply