我有一个电子邮件系统,用户在那里写一条消息并发送消息.
我刚发现的主要问题是考虑这个代码
我刚发现的主要问题是考虑这个代码
$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结果的电子邮件的这个输入:
$74.97 $735.00s
$email消息将导致:
.97 5.00s
我怎样才能解决这个问题?我觉得我的preg_replace模式存在问题.
用户模板可以是任何东西,只要有@@ MESSAGE @@即可,该部分将被更改为用户消息输入.
谢谢
预解析替换文本以在$后面跟一个数字进行转义(记住$n在替换文本中使用时具有特殊含义).请参阅PHP.net文档页面上的
comment:
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);
}
?>