[thelist] Regex back reference problem.

throwaway at gmontague.co.uk throwaway at gmontague.co.uk
Sun Dec 21 15:23:18 CST 2003


Hi all,
  I'm having a problem not with a regex but with the use of backreferences in
the preg_* php functions.  Specifically, my replacement expression looks
something like this.

$replace_with = "\$1$v\$2";

where $v is a 'real' variable and $1 and $2 are the back references.  This works
as expected 99% of the time but if the first character of $v is numeric it gets
truncated.  I assume this is because the character is being evaluated as part of
the prefixing back reference e.g.

$v = '2nd of the month';
$replace_with = "\$1$v\$2";

regex things......well he wants me to output the 12th back reference, then the
string 'nd of the month.' and then the 2nd back reference.

So my question is this:  How do I get php to distingush between $1 followed by
'2nd' and $12 followed by 'nd'?  Currently I'm kludging it by putting spaced
into my replacement string ("\$1 $v$2")  and post processing this space out but
I'm sure there must be a more elegant solution.

Thanks for any help.

gav


More information about the thelist mailing list