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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/767714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:47:28  来源:igfitidea点击:

How to escape $ in PHP using preg_replace?

phpregexpreg-replace

提问by soulmerge

I am using preg_replaceto 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?

如何正确转义美元符号?

回答by soulmerge

I would strongly recommend using preg_quote()instead.

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

回答by Peter Smit

The $ sign has to be escaped with itself so

$ 符号必须自己转义,所以

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

I would also advise to look to addslashesinstead.

我还建议改用addslashes

回答by Sal

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.

这对于需要匹配包含美元符号的字符串的任何人都很有用。

回答by amphetamachine

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('/([?#^&*()$\/])/', '\\', $tmpStr);

回答by katushkin

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 \\\$.

是的,\\$PHP似乎确实将其视为$双引号字符串。这意味着您必须\$通过说\\\$.

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

我刚刚尝试过preg_replace("/\\\$$k\\\$/", $v, $data)并且确实有效($KEY$用 VALUE.

回答by Jake

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

PHP 不是将 \$ 视为 $ 吗?我还没有测试过,它可能是这样的;

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;

我认为,您需要做的是像这样双重转义 $ 字符;

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

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

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

另外 .. 在这种情况下,我会使用 str_replace()

回答by ólafur Waage

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

IIRC 你用 $ 替换 $。所以应该是$$

You can also try

你也可以试试

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

回答by mMo

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

回答by Germán

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

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

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

回答by KingRider

Try a addslashes() ?

尝试一个 addlashes() ?

Check php.net / addslashes()

检查 php.net/addslashes()