【发布时间】:2015-01-01 16:51:45
【问题描述】:
我正在编写一段代码,以帮助我根据从网站上抓取的值创建电子邮件正文。 但我在用值替换占位符时遇到问题。
我的“替换占位符”代码如下所示:
foreach ($products as $productID)
{
$product_array = scrape_product($scrape_url, $productID);
foreach ($product_array as $key => $value)
{
$pattern = '/\#' . $key . '\#/';
$email_body = preg_replace($pattern, $value, $email_body, $product_count);
}
}
函数 scrape_product($scrape_url, $productID);返回多个数组,如下所示:
$product_array = array(
"PRODUCTID" => $ID,
"TITLE" => $productname,
"ONELINER" => $oneliner,
"PRICE" => $price,
"PRICE_ORG" => $price_org,
"DISCOUNT" => $discount,
"IMAGE" => $image,
"URL" => $scrape_url,
);
数组由键值对组成,对于我返回的每个数组,我会遍历我的电子邮件模板,寻找格式为#key# -> 例如#PRODUCTID# 的占位符。
然后我用这个:
$pattern = '/\#' . $key . '\#/';
$email_body = preg_replace($pattern, $value, $email_body, $product_count);
用相应的占位符替换占位符。
所以这是我的问题: 在我的电子邮件模板中,有多次出现相同的占位符(即#PRODUCTID#) - 确切地说,每个数组都有一组完整的占位符。
但我无法获得正确替换占位符的代码...例如:
array1 -> placeholder set1
array2 -> placeholder set2
array3 -> placeholder set3
and so on...
我的系统最多允许 12 个产品 = 12 个数组,其中包含需要放入电子邮件模板的产品信息。但现在它用数组 1 中的值替换占位符,然后完成(因为第一次运行后没有占位符)。
变量 $product_count 保存产品数组的数量(可以是 1-12 之间的任何值)
我已经尝试过使用 strtr() 和 str_replace() 没有任何运气。
我们将不胜感激帮助、输入或提示。
更新 - 添加了我的模板示例
<table width="285" cellspacing="0" cellpadding="0" border="1" style="text-align: left; margin-left: 5px; table-layout: fixed;">
<tbody>
<tr>
<td align="center" width="284" valign="middle" colspan="2">
<a href="#URL##PRODUCTID#/?ssel=false&utm_campaign=%%jobid%%-%%xtyear%%%%xtmonthnumeric%%%%xtday%%&utm_source=newsletter&utm_medium=email&utm_content=#PRODUCTID#" target="_blank">
<img src="#IMAGE#" style="width: 200px; height: 150px; border: 0pt none; margin: 0px;" width="200" height="150" alt="#TITLE#" />
</a>
</td>
</tr>
<tr>
<td colspan="2" height="3" style="font-size:1px; line-height:1%;"> </td>
</tr>
<tr style="font-family: arial; font-size: 15px; color: #363636; font-weight: bold; margin-top: 0px; margin-bottom: 0px;">
<td width="285" valign="top" colspan="2">
#TITLE#
</td>
</tr>
<tr style="font-family: arial; font-size: 15px; font-weight: 100; color: #363636;">
<td height="85" align="left" valign="top" style="height: 85px; font-size: 11px; margin-top: 0px; margin-bottom: 0px;">
<table cellspacing="0" cellpadding="0" align="left" valign="top" style="font-family: arial; text-align-left; font-size: 11px; margin-top: 0px; margin-bottom: 0px;">
<tr>
<td height="3" style="height:3px; font-size:1px; line-height:1%;"> </td>
</tr>
<tr>
<td valign="top" height="54" style="height: 54px;">
#ONELINER#
</td>
</tr>
</table>
</tr>
</tbody>
</table>
<table width="285" cellspacing="0" cellpadding="0" border="1" style="text-align: left; margin-left: 5px; table-layout: fixed;">
<tbody>
<tr>
<td align="center" width="284" valign="middle" colspan="2">
<a href="#URL##PRODUCTID#/?ssel=false&utm_campaign=%%jobid%%-%%xtyear%%%%xtmonthnumeric%%%%xtday%%&utm_source=newsletter&utm_medium=email&utm_content=#PRODUCTID#" target="_blank">
<img src="#IMAGE#" style="width: 200px; height: 150px; border: 0pt none; margin: 0px;" width="200" height="150" alt="#TITLE#" />
</a>
</td>
</tr>
<tr>
<td colspan="2" height="3" style="font-size:1px; line-height:1%;"> </td>
</tr>
<tr style="font-family: arial; font-size: 15px; color: #363636; font-weight: bold; margin-top: 0px; margin-bottom: 0px;">
<td width="285" valign="top" colspan="2">
#TITLE#
</td>
</tr>
<tr style="font-family: arial; font-size: 15px; font-weight: 100; color: #363636;">
<td height="85" align="left" valign="top" style="height: 85px; font-size: 11px; margin-top: 0px; margin-bottom: 0px;">
<table cellspacing="0" cellpadding="0" align="left" valign="top" style="font-family: arial; text-align-left; font-size: 11px; margin-top: 0px; margin-bottom: 0px;">
<tr>
<td height="3" style="height:3px; font-size:1px; line-height:1%;"> </td>
</tr>
<tr>
<td valign="top" height="54" style="height: 54px;">
#ONELINER#
</td>
</tr>
</table>
</tr>
</tbody>
正如您(希望)可以在模板代码中看到的那样,占位符出现了不止一次(例如#ONELINER#)。
我想在第一次遇到占位符时将 #ONELINER# 占位符替换为产品 1 的 oneliner 值。第二次应该是产品 2 的值,依此类推...
希望这是有道理的。
更新
这是一个需要替换到我的模板中的产品数组示例。根据动态的要求。
PRODUCTID -> 50107639XX
TITLE -> Sony XPERIA Z1 Compact
ONELINER -> Det bedste fra Sony i en kompakt, vandtæt smartphone
PRICE -> 2500
PRICE_ORG -> 5000
DISCOUNT -> 50%
IMAGE -> //media.jflindt.dk/image/49377/500/400/sony-xperia-z1-compact-16gb-sort.jpg
URL -> http://www.jflindt.dk/product/
【问题讨论】:
-
添加了模板示例
标签: php arrays preg-replace placeholder