我有以下一段代碼,它將名爲「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(),但它沒有達到預期的效果。
爲了準確,我決定給這個「答案」打勾,雖然derobert的回答也相當準確。順便說一句,什麼是「!」 。 '\ x24'。 「!」位? – Carl 2008-12-03 07:53:57
我不確定!我想它強制正則表達式只匹配'$'符號(這是十六進制ASCII代碼中的24),但我不熟悉那個「!\ x24!」正則表達式... – VonC 2008-12-03 08:06:51