0

the first str_replace works fine but the following two do not process. i tested the replacing variables and the replacing string all are present/echo. do I need a unique $body. for each?

        $body.= "--$mime_boundary\n";
        $body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
        $body.= "Content-Transfer-Encoding: 7bit\n\n";    
        $body.= str_replace("%%user%%",$en['user'],$html_content);
        $body.= str_replace("%%confcode%%",$en['confcode'],$html_content);
        $body.= str_replace("%%memb_id%%",$en['memb_id'],$html_content);    
        $body.= "\n\n";
        $body.= "--$mime_boundary--\n";
3
  • No you don't need unique variables to store the result of str_replace()s results. What does $html_content look like and the values in the $en array? Commented Sep 12, 2013 at 19:02
  • $html_content is an html file $html_content = file_get_contents('/emails/welcome.tpl'); $en['confcode']` and $en['memb_id'] are numbers. I did a echo and the values are present. Commented Sep 12, 2013 at 19:04
  • So what exactly do the second calls return if you echo them to the screen? Commented Sep 12, 2013 at 19:05

3 Answers 3

3

Try

    $body.= str_replace(
        array(
            "%%user%%",
            "%%confcode%%",
            "%%memb_id%%"
        ), 
        array(
            $en['user'],
            $en['confcode'],
            $en['memb_id']
        ),
        $html_content
    );

instead of

    $body.= str_replace("%%user%%",$en['user'],$html_content);
    $body.= str_replace("%%confcode%%",$en['confcode'],$html_content);
    $body.= str_replace("%%memb_id%%",$en['memb_id'],$html_content); 
Sign up to request clarification or add additional context in comments.

Comments

1

http://php.net/manual/en/function.str-replace.php

Try this instead. I think you might have encounter some issues if you are replacing from a beforehand unreplaced value.

$body.= "--$mime_boundary\n";
$body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
$body.= "Content-Transfer-Encoding: 7bit\n\n";    
$body.= str_replace(array("%%user%%","%%confcode%%","%%memb_id%%"),array($en['user'],$en['confcode'],$en['memb_id']),$html_content);
$body.= "\n\n";
$body.= "--$mime_boundary--\n";

Comments

0

I guess, you want to replace ALL Strings within the same $html_content?

There fore you should call replace on the already processed strings in order to get them all working:

    $body.= "--$mime_boundary\n";
    $body.= "Content-Type: text/html; charset=\"UTF-8\"\n";
    $body.= "Content-Transfer-Encoding: 7bit\n\n";    
    $html_content= str_replace("%%user%%",$en['user'],$html_content);
    $html_content= str_replace("%%confcode%%",$en['confcode'],$html_content);
    $body.= str_replace("%%memb_id%%",$en['memb_id'],$html_content);    
    $body.= "\n\n";
    $body.= "--$mime_boundary--\n";

Note, that this will change your $html_content. If thats not desired, use another variable to assign the outcome, or the solution from Mark Baker.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.