我正在研究用于电子邮件系统的模板。然后我将邮件发送给我的客户。
这将是我的create-email.php文件:
并将有一个textarea,我需要一些变量来使用它;
您好{firstname} {lastname},
您的电子邮件地址{email}已添加到我们的新闻通讯列表中。
您可以使用以下链接退订:http : //example.com/unsubscribe.php? memberid = { memberid}&email = {email }
此文本区域将过帐到send-email.php
我的问题从这里开始。我想用来自数据库的数据替换{firstname} {lastname} {email} {memberid}。
我怎样才能做到这一点 ?
将str_replace与值数组一起使用。
// $array can be set from a query perhaps?
$array = array('firstname' => 'Bob', 'lastname' => 'Dave',...);
// $template could also be a string which is a large block such as yours above.
$template = file_get_contents($template);
foreach($array as $key => $value) {
$template = str_replace("{$key}", $value, $template);
}
您也可以使用两个数组来完成此操作,但是我实际上并不主张这样做,因为它使用更多的内存,并且在跟踪哪个字段与哪个值匹配时会出现问题。
// Array of fields to replace
$fields = array('firstname', 'lastname',...);
$values = array('bob', 'dave',...);
$template = file_get_contents($template);
$template = str_replace($fields, $values, $template);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句