title: Replace multiple placeholders with PHP?
body: <p>I have a function that sends out site emails (using phpmailer), what I want to do is basically for php to replace all the placheholders in the email.tpl file with content that I feed it. The problem for me is I don't want to be repeating code hence why I created a function (below).</p>
  
  <p>Without a php function I would do the following in a script</p>
  
  <pre><code>// email template file
  $email_template = "email.tpl";
  
  // Get contact form template from file
  $message = file_get_contents($email_template);
  
  // Replace place holders in email template
  $message = str_replace("[{USERNAME}]", $username, $message);
  $message = str_replace("[{EMAIL}]", $email, $message);
  </code></pre>
  
  <p>Now I know how to do the rest but I am stuck on the <code>str_replace()</code>, as shown above I have multiple <code>str_replace()</code> functions to replace the placeholders in the email template. What I would like is to add the <code>str_replace()</code> to my function (below) and get it to find all instances of <code>[\]</code> in the email template I give it and replace it with the placeholders values that I will give it like this: <code>str_replace("[\]", 'replace_with', $email_body)</code></p>
  
  <p>The problem is I don't know how I would pass multiple placeholders and their replacement values into my function and get the <code>str_replace("[{\}]", 'replace_with', $email_body)</code> to process all the placeholders I give it and replace with there corresponding values. </p>
  
  <p>Because I want to use the function in multiple places and to avoid duplicating code, on some scripts I may pass the function 5 placeholders and there values and another script may need to pass 10 placeholders and there values to the function to use in email template.</p>
  
  <p>I'm not sure if I will need to use an an array on the script(s) that will use the function and a <code>for</code> loop in the function perhaps to get my php function to take in xx placeholders and xx values from a script and to loop through the placeholders and replace them with there values.</p>
  
  <p>Here's my function that I referred to above. I commented the script which may explain much easier.</p>
  
  <pre><code>// WILL NEED TO PASS PERHAPS AN ARRAY OF MY PLACEHOLDERS AND THERE VALUES FROM x SCRIPT
  // INTO THE FUNCTION ?
  function phpmailer($to_email, $email_subject, $email_body, $email_tpl) {
  
  // include php mailer class
  require_once("class.phpmailer.php");
  
  // send to email (receipent)
  global $to_email;
  // add the body for mail
  global $email_subject;
  // email message body
  global $email_body;
  // email template
  global $email_tpl;
  
  // get email template
  $message = file_get_contents($email_tpl);
  
  // replace email template placeholders with content from x script
  // FIND ALL INSTANCES OF [{}] IN EMAIL TEMPLATE THAT I FEED THE FUNCTION 
  // WITH AND REPLACE IT WITH THERE CORRESPOING VALUES.
  // NOT SURE IF I NEED A FOR LOOP HERE PERHAPS TO LOOP THROUGH ALL 
  // PLACEHOLDERS I FEED THE FUNCTION WITH AND REPLACE WITH THERE CORRESPONDING VALUES
  $email_body       = str_replace("[{\}]", 'replace', $email_body);
  
  // create object of PHPMailer
  $mail = new PHPMailer();
  
  // inform class to use smtp
  $mail-&gt;IsSMTP();
  // enable smtp authentication
  $mail-&gt;SMTPAuth   = SMTP_AUTH;
  // host of the smtp server
  $mail-&gt;Host       = SMTP_HOST;
  // port of the smtp server
  $mail-&gt;Port       = SMTP_PORT;
  // smtp user name
  $mail-&gt;Username   = SMTP_USER;
  // smtp user password
  $mail-&gt;Password   = SMTP_PASS;
  // mail charset
  $mail-&gt;CharSet    = MAIL_CHARSET;
  
  // set from email address
  $mail-&gt;SetFrom(FROM_EMAIL);
  // to address
  $mail-&gt;AddAddress($to_email);
  // email subject
  $mail-&gt;Subject = $email_subject;
  // html message body
  $mail-&gt;MsgHTML($email_body);
  // plain text message body (no html)
  $mail-&gt;AltBody(strip_tags($email_body));
  
  // finally send the mail
  if(!$mail-&gt;Send()) {
    echo "Mailer Error: " . $mail-&gt;ErrorInfo;
    } else {
    echo "Message sent Successfully!";
    }
  }
  </code></pre>