gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:49:51 26 4
gpt4 key购买 nike

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

    $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 在替换文本中使用时具有特殊含义)。查看comment在 php.net 文档页面上:

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);
}
?>

关于php - preg_replace 导致美元符号被删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18993863/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com