简体   繁体   English

如何使用preg_replace在PHP中转义$?

[英]How to escape $ in PHP using preg_replace?

I am using preg_replace to escape special characters: 我使用preg_replace来转义特殊字符:

$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? 如何正确逃避美元符号?

我强烈建议使用preg_quote()代替。

The correct answer is that you must escape the backslash and the dollar sign in the regex using PHP's escape characters. 正确的答案是你必须使用PHP的转义字符来逃避正则表达式中的反斜杠和美元符号。

backslash = \\
dollar sign = \$

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

This is useful for anyone that needs to match a string that contains a dollar sign. 这对于需要匹配包含美元符号的字符串的任何人都很有用。

The $ sign has to be escaped with itself so $ sign必须自行转义

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

I would also advise to look to addslashes instead. 我还建议改为使用addslashes

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: PHP中的单引号( ' )与双引号( " )的工作方式不同。它与Perl非常相似,其中变量插值不会出现在单引号字符串中,而美元符号( $ )不是元字符:

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

Also, Perl's character classes will simplify your code: 此外,Perl的字符类将简化您的代码:

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

Yes, it does seem that \\\\$ is seen by PHP as $ in a double-quoted string. 是的,它似乎是\\\\$由PHP视为$的双引号字符串。 That means you have to make PHP see a \\$ by saying \\\\\\$ . 这意味着你必须通过说\\\\\\$让PHP看到\\$ \\\\\\$

I just tried preg_replace("/\\\\\\$$k\\\\\\$/", $v, $data) and indeed it works (replaces occurrences of $KEY$ with VALUE. 我刚刚尝试了preg_replace("/\\\\\\$$k\\\\\\$/", $v, $data)并且确实有效(用VALUE替换$KEY$出现次数)。

$pattern = preg_replace('/\$(.+)/', '\\\$$1', $pattern);

IIRC you replace $ with $. IIRC你用$替换$。 So it should be $$ 所以它应该是$$

You can also try 你也可以试试

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

isn't it true that PHP sees \\$ as $ ? PHP认为\\ $为$是不是真的? 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 php是第一个,并用“/ $ /”替换你的“/ \\ $ /”然后preg引擎做了它的魔术..不幸的是,$是一个正则表达式运算符(我相信它匹配字符串的结尾?),所以它没有在你的文本中找到$ -characters,但会

I think, what you need to do, is to doubble escape the $-character like so; 我认为,你需要做的是,能够躲过这样的$ -character;

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

Also .. in this case, I would have just used str_replace() 另外..在这种情况下,我会使用str_replace()

在PHP中,对于HTML中使用的“$”的特定情况,您还可以为其实体执行先前的替换:

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

Try a addslashes() ? 尝试使用addslashes()?

Check php.net / addslashes() 检查php.net / addslashes()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM