加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:57:22 所属栏目:PHP教程 来源:网络整理
导读:我有一个电子邮件系统,用户在那里写一条消息并发送消息. 我刚发现的主要问题是考虑这个代码 $findEmail = $this-Data-field('body',array('id' = 1610)); //$getUserEmailTemplate will take frm dbase and e.g: //Hi,@@MESSAGE@@. From: StackOverflow //It
我有一个电子邮件系统,用户在那里写一条消息并发送消息.
我刚发现的主要问题是考虑这个代码
$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);
  }
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读