preg_replace causing dollar signs get removed(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 导致美元符号被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:preg_replace 导致美元符号被删除


- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01