問題描述
我有一個電子郵件系統,用戶可以在其中寫一條消息,然后發送消息.我剛剛發現的主要問題,考慮這段代碼
I have an email system, where user write a message and it will send the message. The main problem which I just found, consider this code
$findEmail = $this->Data->field('body', array('id' => 1610));
//$getUserEmailTemplate will take frm dbase and e.g:
//Hi, @@MESSAGE@@. From: StackOverflow
//It should change @@MESSAGE@@ part to data from $findEmail (in this example is the $74.97 ...)
$getUserEmailTemplate = $findUser['User']['email_template'];
$emailMessage = preg_replace('/B@@MESSAGE@@B/u', $findEmail, $getUserEmailTemplate);
debug($findEmail);
debug($emailMessage);
并為 $findemail 結果的電子郵件考慮此輸入:
and consider this input for the email for $findemail result:
$74.97
$735.00s
$email 消息將導致:
$email Message will result in:
.97
5.00s
我該如何解決這個問題?我覺得我的 preg_replace 模式有問題.
How can I fix this? I feel like there's problem with my preg_replace pattern.
用戶模板可以是任何東西,只要有@@MESSAGE@@,那部分就變成了用戶消息輸入.
User template can be anything, as long as there is @@MESSAGE@@ which, that part will be changed to the user message input.
謝謝
推薦答案
預解析替換文本以在后跟數字時轉義 $
(記住 $n
> 在替換文本中使用時具有特殊含義).請參閱 php.net 文檔頁面上的評論:
Pre-parse the replacement text to escape the $
when followed by a number (remember that $n
has special meaning when using in the replacement text). See the comment on the php.net docs page:
如果您的替換文本有可能包含任何字符串,例如$0.95",你需要避開那些 $n 反向引用:
If there's a chance your replacement text contains any strings such as "$0.95", you'll need to escape those $n backreferences:
<?php
function escape_backreference($x){
return preg_replace('/$(d)/', '\$$1', $x);
}
?>
這篇關于preg_replace 導致美元符號被刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!