note 20214 deleted from function.preg-replace by sniper

From: Date: Sun, 07 Sep 2003 22:08:57 +0000
Subject: note 20214 deleted from function.preg-replace by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-55716@lists.php.net to get a copy of this message
Note Submitter: cbakerATcbakerDOTorg ---- Since, as far as I can tell, the preg_replace() replacement parameter can't deal with using $1 as a key in an existing hash, here's an ugly hack that does just that: $string = "asdf[[foo]]asdf[[bar]]asdf"; $args = array("foo"=>"BAR", "bar"=>"PACO"); while(preg_match("/^(.*)\[\[(\w+)\]\](.*)$/", $string, $match)) { $string = $match[1] . $args[$match[2]] . $match[3]; } It would be nice if $string = preg_replace("/\[\[(\w+)\]\]/", $args["$1"], $string); worked but it don't.

« previous php.notes (#55716) next »