0

I am working on a piece of code to help me create an email body based on values scraped from a website. But I am having problems replacing placeholders with values.

My "replace placeholder" code looks like this:

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

The function scrape_product($scrape_url, $productID); returns a number of arrays that looks like this:

$product_array = array(
    "PRODUCTID" => $ID,
    "TITLE"     => $productname,
    "ONELINER"  => $oneliner,
    "PRICE"     => $price,
    "PRICE_ORG" => $price_org,
    "DISCOUNT"  => $discount,
    "IMAGE"     => $image,
    "URL"       => $scrape_url,
);

The arrays consist or key value pairs and for each array I get back I loop through my email template looking for a placeholder in the format #key# -> for example #PRODUCTID#.

I then use this:

$pattern = '/\#' . $key . '\#/';
$email_body = preg_replace($pattern, $value, $email_body, $product_count);

To replace the placeholders with the corresponding placeholder.

So here is my problem: In my email template there are are several occurrences of the same placeholder (i.e #PRODUCTID#) - to be exact there is a complete set of placeholders for each array.

But I cannot get the code to replace the placeholders correctly... like:

array1 -> placeholder set1
array2 -> placeholder set2
array3 -> placeholder set3
and so on...

My system allows up to a total of 12 products = 12 arrays with product info that needs to be put in the email template. But right now it replaces the placeholder with the values from array 1 and then finished (because there are no placeholders left after the first runthrough).

The variable $product_count holds the number of product arrays (can be anything from 1-12)

I have already tried using strtr() and str_replace() without any luck.

Help, input or hints would be greatly appreciated.

UPDATE - added an example of my template

<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>

As you (hopefully) can see in the template code the placeholders appear more than once (for instance #ONELINER#).

I want to replace the #ONELINER# placeholder with the oneliner value for product 1 the first time I meet the placeholder. The second time it should be the value for product 2 and so on...

Hope this makes sense.

UPDATE

Here is an example of an product array that needs to be replaced into my template. As requested by dynamic.

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

1 Answer 1

1

Actually for such simple replacing you don't need preg. You can use str_replace();

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

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

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

As Amal pointed out, if you have an array of pairs:

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

You can use strtr();

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

Altho I personally prefer str_replace due to its name. If you need to build the $search and $replace arrays, having a single array of $pairs, you can do:

$search = array_keys($pairs);
$replace = array_values($pairs);
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried str_replace() and strtr() already and they do not not solve the problem... the closest I have gotten is with preg_replace.
then you was not clear in the question. Post a snippet of your text that need to be replaced and a print_r of your values
dynamic - I have added a sample of my template and a sample of a product array
jflindt, that is wrong by design. You should have a template of your item (I if understood correctly it is a full table). Then you should append each table with consecutive call to str_replace
dynamic - I see your point... I'll try populating the product tables before adding them to the email body...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.