2009/09/14

how to ignore dollar sign in the replacement

Found the solution @ http://theserverpages.com/php/manual/en/function.preg-replace.php

If you're setting your replacement values elsewhere and your replacement value can contain a dollar sign followed by an integer eg:

$replacement = 'Dad's $7 shoes'

You'll find that preg_replace() treats this (quite rightly) as callback syntax.

So, you might want to do this:

$replacement = preg_replace("!" . '\x24' . "!" , '\\\$' , $replacement);

To your replacement string first; Putting in an escaped backslash '\\' followed by an escaped dollar sign '\$' where every instance appears.

No comments:

Post a Comment