微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – preg_replace导致美元符号被删除

我有一个电子邮件系统,用户在那里写一条消息并发送消息.
我刚发现的主要问题是考虑这个代码
$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);
  }
?>

原文地址:https://www.jb51.cc/php/134108.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐