2014-11-05 122 views
0

我正在研究一段代码,以帮助我根据从网站上获取的值创建电子邮件正文。 但我有问题用值替换占位符。PHP - 在循环数组的同时替换多组占位符

我的 “替代占位符” 代码如下所示:

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, 
); 

该阵列由或键值对,并为每个阵列我回去我依次通过我的电子邮件模板寻找格式#键#占位符 - >的例如#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中的值替换为占位符,然后结束(因为在第一个runthrough后没有剩下占位符)。

变量$ 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#)。

我想在第一次遇到占位符时用产品1的oneliner值替换#ONELINER#占位符。第二次,它应该是产品2的价值等等......

希望这是有道理的。

UPDATE

下面是一个产品阵列需要更换成我的模板的一个例子。按照动态要求。

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/ 
+0

添加模板样本 – JFlindt 2014-11-05 13:43:11

回答

1

其实这样简单的替换你不需要preg。您可以使用str_replace();

$search = array(
    '#PRODUCTID#', 
    '#TYPE#', 
    '#CAT#' 
); 

$replace = array(
    'val1', 
    'val2', 
    'val3' 
); 

$text = str_replace($search, $replace, $text); 

作为阿迈勒指出的那样,如果你有对的数组:

$pairs = array(
    "#PRODUCTID#" => $ID, 
    "#TITLE#"  => $productname, 
    "#ONELINER#" => $oneliner 
); 

您可以使用strtr();

$text = strtr($text, $pairs); 

本书虽然是我个人比较喜欢str_replace由于其名称。如果您需要构建$search$replace阵列,具有$pairs一个数组,你可以这样做:

$search = array_keys($pairs); 
$replace = array_values($pairs); 
+1

'str_replace()函数'是你的朋友,平时。 – David 2014-11-05 13:19:05

+1

或['strtr()'](http://php.net/strtr)。 – 2014-11-05 13:20:52

+0

我已经尝试过str_replace()和strtr(),并且它们不会解决问题......最近的问题是使用preg_replace。 – JFlindt 2014-11-05 13:35:48