preg_replace 导致美元符号被删除

2023-01-19php开发问题
7

本文介绍了preg_replace 导致美元符号被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个电子邮件系统,用户可以在其中写一条消息,然后发送消息.我刚刚发现的主要问题,考虑这段代码

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 导致美元符号被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17