如何在 preg_replace 中保留数字和特殊字符

How to keep digit and special character in preg_replace

我想用 45 美元等价格更换 {goal} 牙套。例如

 $pattern = /\{goal\}/

 $replacement = .00

 $subject = Final price is {goal}

所以函数看起来像

 preg_replace('/\{goal\}/', '.00', 'Free shipping for all orders over {goal}');

实际输出应该是

Final price is .00

但我得到的输出是

 Final price is .00

所以看起来 preg_replace 用空白字符串替换了整个数字和特殊字符。有什么办法可以保留吗

The $n will be replaced by the text captured by the n'th parenthesized pattern. manual

当您使用 '.00' php 时,将其视为第 45 个捕获组。所以你应该通过 \ 逃避 $ 来解决问题

preg_replace('/\{goal\}/', '$45.00', 'Free shipping for all orders over {goal}');