View Full Version : Backreferences in text being used in regular expressions.

05-06-2006, 01:04 AM
Sometimes I have $1 in text, which are being parsed by regular expressions. The regular expressions are taking the $1 in the text literally. e.g.

$text = "$1 cookie.";
$replace = "cookie from $2.";
$text = preg_replace("/cookie/i", $replace, $text);

print $text;

Text will output "$1 cookie from ..". How can I avoid this?

05-06-2006, 04:37 AM
Figured it out:

while (preg_match($pattern, $text, $match))
$text = str_replace($match[0], $replacement, $text);