相关疑难解决方法(0)

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

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); 
Run Code Online (Sandbox Code Playgroud)

但是我无法$使用相同的功能逃脱:

$tmpStr=preg_replace("/\$/", "\$", $tmpStr);
Run Code Online (Sandbox Code Playgroud)

而且当我使用上面的语句时,所有空格都被替换为$并且$没有被转义.

如何正确逃避美元符号?

php regex preg-replace

6
推荐指数
3
解决办法
2万
查看次数

标签 统计

php ×1

preg-replace ×1

regex ×1