【发布时间】:2008-12-03 07:30:07
【问题描述】:
我有以下代码,它将“模板标记”(例如 %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() 但它没有达到预期的效果。
【问题讨论】: