Select 多次用数组中的每个值替换一个单词

Select multiple replace one word with every value in an array

好的,我有一个联系表格,我希望人们 select 多个项目,一旦他们提交联系表格,我希望它使用 html 模板发送电子邮件。我已将其设置为字符串替换 html 模板中的数据,但每次我尝试执行数组时,它都会说数组或仅显示 selected 的多个项目之一。

这是 HTML Select 代码,我确保添加 [] 以使名称成为数组。

<select class="js-example-multiple js-states form-control" multiple="multiple" name="product[]">
   <option value="Baby Shark Castle 15ft x 18ft">Baby Shark Castle 15ft x 18ft</option>
   <option value="Pirate's assault course 12ft x 25ft">Pirate's assault course 12ft x 25ft</option>
   <option value="Yellow Mega Slide 18ftx18ft">Yellow Mega Slide 18ftx18ft</option>
   <option value="18ft x 18ft Disco Dome Lights &amp; Speaker">18ft x 18ft Disco Dome Lights &amp; Speaker</option>
   <option value="Assault Course 35ft Long 12 ft Wide">Assault Course 35ft Long 12 ft Wide</option>
   <option value="Inflatable Nightclub 12ft x 15ft">Inflatable Nightclub 12ft x 15ft</option>
   <option value="40ft Assault course 15ft x 40ft">40ft Assault course 15ft x 40ft</option>
   <option value="Inflatable Pub 17x17 - Holds 20 People">Inflatable Pub 17x17 - Holds 20 People</option>
</select>

这是 php 代码,我已成功替换其他单个值,但是当我尝试用多个值替换一个值时,它只显示一个值。我尝试了一个 foreach 循环,但这只在我回显价值 $product 时有效。我想将所有项目串起来 selected 而不仅仅是一个,我有一个我希望它看起来像下面的例子。

    // Bring in the email template here
    $html = file_get_contents('template.html');

    // You only need to modify the following three lines of code to customise your form to mail script.
    $email_to = "aaron@pugmanmedia.co.uk";          // Specify the email address you want to send the mail to.
    $email_from = "info@pugmanmedia.co.uk";  //Specify the email address that you want to send the email from. This needs to be Fasthosts hosted,
    $email_subject = "Website Contact Form";    // Set the subject of your email.
    // Specify a page on your website to display a thankyou message when the mail is sent
    $thankyou_url = "../thankyou.html";

    // Get the details the user entered into the form
    $name = $_POST["name"];
    $reply_to = $_POST["email"];
    $number = $_POST["number"];
    $date = $_POST["date"];
    $message = $_POST["message"];
    $products = $_POST["product"];   
    

    // Validate the email address entered by the user
    if(!filter_var($email_from, FILTER_VALIDATE_EMAIL)) {
        // Invalid email address
        die("The email address entered is invalid.");
    }

    // Replacing the details in the template the above variables
    $html =  str_replace("{{username}}",$name,$html);
    $html =  str_replace("{{email}}",$reply_to,$html);
    $html =  str_replace("{{number}}",$number,$html);
    $html =  str_replace("{{date}}",$date,$html);
    $html =  str_replace("{{message}}",$message,$html);
    foreach($products as $product){
        $list = $product . "<br> test <br>";
        $html =  str_replace("{{list}}",$list,$html);
    };

这是html模板代码

 <div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px;">
        {{list}}
    </p>
 </div>

这是我得到的结果

 <div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px;">
        Pirate's assault course 12ft x 25ft
    </p>
 </div>

我想要的最终结果是当人们 select 任何数量的项目都出现在一个地方,而不是上面

<div style="margin: 0px; padding: 0px;">
    <p style="margin: 0px; padding: 0px;">
       Baby Shark Castle 15ft x 18ft
       Assault Course 35ft Long 12 ft Wide
       Pirate's assault course 12ft x 25ft
       and so on
    </p>
 </div>

当你这样做时:

foreach($products as $product){
    $list = $product . "<br> test <br>";
    $html =  str_replace("{{list}}",$list,$html);
};

你实际上做了n次替换,但只有第一次有效,因为之后就没有更多的“{{list}}”需要替换了。这不是错误,只是不是您所期望的。

这样试试:

$html =  str_replace("{{message}}",$message,$html);
$list = '';
foreach($products as $product){
    $list .= $product . "<br>";
};
$html =  str_replace("{{list}}",$list,$html);

现在您正在为所有选定的产品构建一个临时字符串变量,然后您只替换它一次。

你需要implode

替换

foreach($products as $product){
    $list = $product . "<br> test <br>";
    $html =  str_replace("{{list}}",$list,$html);
};

$list = implode("<br>",$products);
$html =  str_replace("{{list}}",$list,$html);