Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何阻止str_replace更改数组中每个项中的所有实例?_Php_Regex_Str Replace - Fatal编程技术网

Php 如何阻止str_replace更改数组中每个项中的所有实例?

Php 如何阻止str_replace更改数组中每个项中的所有实例?,php,regex,str-replace,Php,Regex,Str Replace,我正在循环一个多维数组和for循环,并为循环中的每个项目触发和发送电子邮件。我想将{{fname}}从密钥更改为实名 foreach($customermarketingarray as $key => $value){ $customeremail = $value['email']; $fname = $value['first_name']; $body = str_replace("{{fname}}",$fname,$body);

我正在循环一个多维数组和for循环,并为循环中的每个项目触发和发送电子邮件。我想将{{fname}}从密钥更改为实名

foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $body = str_replace("{{fname}}",$fname,$body);                     
                             }

出于某种原因,除了{{fname}}在第一个循环中为循环中的每个fname使用第一个名称之外,所有内容都是唯一的。如果第一人称是Joe,那么当你第一次使用str_replace时,它会使每个fname Joe替换掉整个身体中出现的{{fname}。在进一步的循环迭代中没有什么可以替换的

使用preg_replace,它具有额外的参数(限制),该参数说明最多进行多少次替换。在您的示例中,如果每个名称只有一个{{fname}},则使用1作为计数参数即可

阅读文档。这是不言自明的。

另一个问题是数据结构的问题。如果没有其他规则,您无法区分哪个{{fname}}到哪个替换


不清楚是否要使用$body作为模板(我假设为否,因为除了循环迭代中的替换之外,您没有使用$body进行任何操作)。如果正文中多次出现{fname},并且您希望用循环迭代中的名称逐个替换它们,则我的答案是有效的。

第一次使用stru replace时,它将替换整个正文中出现的{fname}。在进一步的循环迭代中没有什么可以替换的

使用preg_replace,它具有额外的参数(限制),该参数说明最多进行多少次替换。在您的示例中,如果每个名称只有一个{{fname}},则使用1作为计数参数即可

阅读文档。这是不言自明的。

另一个问题是数据结构的问题。如果没有其他规则,您无法区分哪个{{fname}}到哪个替换


不清楚是否要使用$body作为模板(我假设为否,因为除了循环迭代中的替换之外,您没有使用$body进行任何操作)。如果{fname}在body中多次出现,并且您希望用循环迭代中的名称逐个替换它们,那么我的答案是有效的。

因为您正在反复使用一个
$body
,一旦替换完成,就没有什么可替换的了。在每次迭代中使用新版本的
$body

foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $tempBody = str_replace("{{fname}}",$fname,$body);              
      // now use this $tempBody for display
     }

这样,在循环的每次迭代中,您都会再次从
$body
获得一个新模板,您可以对其进行替换,然后使用它

因为您反复使用一个
$body
,一旦更换了,就没有什么可更换的了。在每次迭代中使用新版本的
$body

foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $tempBody = str_replace("{{fname}}",$fname,$body);              
      // now use this $tempBody for display
     }

这样,在循环的每次迭代中,您都会再次从
$body
获得一个新模板,您可以对其进行替换,然后使用它

使用preg_替换

$body_message = $body;
foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $body_message = str_replace("{{fname}}",$fname,$body);                        
     }
可以使用preg_replace代替str_replace 答复:

检查并检查弃用的

使用str_替换

$body_message = $body;
foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $body_message = str_replace("{{fname}}",$fname,$body);                        
     }

使用preg_替换

$body_message = $body;
foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $body_message = str_replace("{{fname}}",$fname,$body);                        
     }
可以使用preg_replace代替str_replace 答复:

检查并检查弃用的

使用str_替换

$body_message = $body;
foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $body_message = str_replace("{{fname}}",$fname,$body);                        
     }

您正在替换每个循环中的变量$body。只需使用一个新变量来存储结果。请参见下面的示例

<?php
$customermarketingarray=array(
    0=>array("email"=>"1@test.com","first_name"=>"Joe1"),
    1=>array("email"=>"2@test.com","first_name"=>"Joe2"),
    2=>array("email"=>"3@test.com","first_name"=>"Joe3"),
    3=>array("email"=>"4@test.com","first_name"=>"Joe4")
);
$body="First Name: {{fname}}<br>Email: {{email}}<br><br>";
foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $email = $value['email'];
     $res_body .= str_replace(array("{{fname}}","{{email}}"),array($fname,$email),$body);
}
echo $res_body;
?>

您正在替换每个循环中的变量$body。只需使用一个新变量来存储结果。请参见下面的示例

<?php
$customermarketingarray=array(
    0=>array("email"=>"1@test.com","first_name"=>"Joe1"),
    1=>array("email"=>"2@test.com","first_name"=>"Joe2"),
    2=>array("email"=>"3@test.com","first_name"=>"Joe3"),
    3=>array("email"=>"4@test.com","first_name"=>"Joe4")
);
$body="First Name: {{fname}}<br>Email: {{email}}<br><br>";
foreach($customermarketingarray as $key => $value){
     $customeremail = $value['email'];
     $fname = $value['first_name'];
     $email = $value['email'];
     $res_body .= str_replace(array("{{fname}}","{{email}}"),array($fname,$email),$body);
}
echo $res_body;
?>

然后是你的数据问题。然后是你的数据问题。天啊。。。把它和preg_replace参数弄错了-马上纠正我的答案:)天哪。。。与preg_replace参数错误-立即更正我的答案:)