从文本文件中获取内容并替换关键字PHP


Get Contents from Text File and Replace Keywords PHP

我有一个表单。我使用"post"方法向多个注册的人发送和发送电子邮件。然后电子邮件的$body是基于模板的。没有数据库,没有类,简单的表单。是的,我已经读过了,它们都在那里,我只是无法把它们放在一起,与这个案例有关。

文本"email_template.txt"应该有类似的内容:

Hello #firstname# #lastname#. Thank you for registering! Your registered email is #email#

经过PHP 处理后会是这样

Hello **John Doe**. Thank you registering! Your registered email is **example@example.com**.

在我的php上,我有这样的东西:

<?php 
//form and validation
$firstname = "John"; // inputed name on the form, let say
$lastname = "Doe"; // inputed last name
$email = "example"; // inputed email 
//email message
$body = ... some type of a get_file_content....
mail($_POST['email'], 'Party Invitation', $body, 'From: admin@admin.com');
?>

其中$body是通过此PHP表单提交给注册者的电子邮件。

sprintf将很好地进行

你的模板可以是:

你好%s%s。感谢您的注册!您注册的电子邮件是%s

$body=sprintf($fileContents,$firstname,$lastname,$email);

str_replace(),几乎是一个查找替换。

$body=";你好#firstname##lastname#。感谢您的注册!您注册的电子邮件是"#email#";;

$body=str_replace("#firstname#",$firstname,$body);

$body=str_replace("#lastname#",$lastname,$body);

$body=str_replace("#email#",$email,$body);