[pmwiki-devel] help with preg_replace()

Ben Stallings ben at interdependentweb.com
Thu Mar 15 11:39:35 CDT 2007


Regular expressions make my eyes cross.  Can someone help me with this 
puzzle?

I want to take a string and replace all occurrences of [var] (i.e. any 
single word enclosed in single brackets) with DQquote($_REQUEST['var']) 
(i.e. any post, get, or cookie data with that variable name, passed 
through a function called DQquote).

My previous strategy was to do a foreach($_REQUEST as $k => $v), which 
works great for the variables that are defined, but it leaves the 
undefined ones unchanged, and that turns out to be unacceptable.  I 
suppose I could follow this incomplete technique up with a preg_replace 
to get rid of the non-matching variables, but it would be slicker to do 
it all in one swell foop.

Can I do this with just preg_replace, or do I need to do a 
preg_match_all to get the patterns first, then build an array of 
replacements, then replace them?  Suggestions please!  Thanks in 
advance.  --Ben



More information about the pmwiki-devel mailing list