12

I am using preg_replace to escape special characters:

$tmpStr=preg_replace("/\?/", "\?", $tmpStr);
$tmpStr=preg_replace("/\#/", "\#", $tmpStr);
$tmpStr=preg_replace("/\^/", "\^", $tmpStr);
$tmpStr=preg_replace("/\&/", "\&", $tmpStr);
$tmpStr=preg_replace("/\*/", "\*", $tmpStr);
$tmpStr=preg_replace("/\(/", "\(", $tmpStr);
$tmpStr=preg_replace("/\)/", "\)", $tmpStr);
$tmpStr=preg_replace("/\//", "\/", $tmpStr); 

But I am not able to escape $ using the same function:

$tmpStr=preg_replace("/\$/", "\$", $tmpStr);

And also when I use the above statement all the spaces get replaced by $ and $ is not getting escaped.

How do I escape the dollar sign correctly?

10 Answers 10

14

I would strongly recommend using preg_quote() instead.

6
  • 1
    I would find this answer more helpful if it gave the rationale for using preg_quote instead of preg_replace. Also, it would be nice to know why the preg_replace approach didn't work for escaping the dollar sign. Oct 19, 2016 at 20:52
  • 1
    @RandallStewart "preg_quote instead of preg_replace"? Those two functions are not replaceable, you can't use one instead of the other, you use preg_quote to escape values in regular expression strings (used inside of preg_replace) Jul 27, 2017 at 20:05
  • This will produce bugs in other places. For example ‌‌preg_replace('/\{\s*price\s*\}/', preg_quote('$100.00'), 'it cost {price}'); will return ‌it cost $100\.00 instead of the expected ‌it cost $100.00 May 9, 2018 at 23:12
  • 2
    @PeterAjtai: I think you misunderstood the purpose of preg_quote(): It is not for the second parameter of preg_replace(), but for the first. Please re-read the question and the documentation of preg_quote() (the second example might be especially useful for fully understanding the issue at hand)
    – soulmerge
    May 14, 2018 at 7:23
  • Thanks @soulmerge I see the difference. May 15, 2018 at 3:41
9

The correct answer is that you must escape the backslash and the dollar sign in the regex using PHP's escape characters.

backslash = \\
dollar sign = \$

$tmpStr=preg_replace("/\\\$/", "\\$", $tmpStr);

This is useful for anyone that needs to match a string that contains a dollar sign.

1
  • 1
    I think this should be the correct answer to the question also with the explanation, because in my case also preg_quote had a problem with replacing with the dollar sign and this double escaped version solved it.
    – Mochi
    Jun 22, 2020 at 12:14
5

Looks like your problem is one of escaping. Single quotes (') in PHP work differently than double quotes ("). It's a lot like in Perl, where variable interpolation does not happen in singly-quoted strings, and the dollar sign ($) is not a meta-character:

print "\$"; # prints $
print '\$'; # prints \$

Also, Perl's character classes will simplify your code:

$tmpStr = preg_replace('/([?#^&*()$\\/])/', '\\\\$1', $tmpStr);
4

The $ sign has to be escaped with itself so

$tmpStr=preg_replace("/$$/", "\$", $tmpStr);

I would also advise to look to addslashes instead.

2

Yes, it does seem that \\$ is seen by PHP as $ in a double-quoted string. That means you have to make PHP see a \$ by saying \\\$ .

I just tried preg_replace("/\\\$$k\\\$/", $v, $data) and indeed it works (replaces occurrences of $KEY$ with VALUE.

0

IIRC you replace $ with $. So it should be $$

You can also try

$tmpStr=preg_replace('/\$/', '\$', $tmpStr);
0
0

isn't it true that PHP sees \$ as $ ? I haven't tested this out, it might go like this;

php is first, and replaces your "/\$/" with "/$/" then the preg engine does it's magic .. unfortunately, $ is a regular expression operator ( I believe it matches the end of a string?), so it doesn't find the $-characters in your text but will

I think, what you need to do, is to doubble escape the $-character like so;

$tmpStr=preg_replace("/\$/", "\$", $tmpStr);

Also .. in this case, I would have just used str_replace()

-1
$pattern = preg_replace('/\$(.+)/', '\\\$$1', $pattern);
1
  • Could you add some details regarding your code and it how addresses the problem(s) in the question? Code only answers are not very useful, especially for future readers that stumble upon this post. Thanks,
    – Cristik
    Mar 17, 2016 at 8:06
-2

In PHP, for the particular case of "$" used in HTML, you can also do a previous replace for its entity:

$tmpStr = str_replace('$', '$',$tmpStr);
-2

Try a addslashes() ?

Check php.net / addslashes()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.