[英]preg_replace is replacing $ signs
我有以下代码用名为$ post_title的变量的内容替换“模板标记”(例如%POST_TITLE%)。
function replaceTags( $template, $newtext ) {
$template = preg_replace( '/%MYTAG%/', $newtext, $template );
return $template;
}
问题是,当$ post_full中包含“ $”时,返回的结果将被删除。 例如:
$template = "Replace this: %MYTAG";
$newtext = "I earn $1,000,000 a year";
print replaceTags( $template, $newtext );
// RESULT
Replace this: I earn ,000,000 a year";
我知道这与在$ newtext中正确转义$ 1有关。 我尝试使用preg_quote(),但没有达到预期的效果。
嗯,由于您实际上并未在其中使用正则表达式,为什么不直接使用str_replace
呢? 这样会更快,而且您不会遇到像这样的怪异问题。
根据preg_replace手册 ,preg_replace()将此( $1
)视为后向引用 。
(而不是preg_replace手册页的注释中提到的“
回调语法
”。
谢谢Jan Goyvaerts )。
$newtext = preg_replace("!" . '\x24' . "!" , '\\\$' , $newtext );
应该照顾你的“ $”号
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.