...

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



wyrd33
05-06-2006, 12: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?

wyrd33
05-06-2006, 03:37 AM
Figured it out:



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum