Replace multiple PHP placeholders?

I have a function that sends emails from the site (using phpmailer), what I want to do, mainly for php, is to replace all the plachholders in the email.tpl file with the content that I feed it. The problem for me is that I do not want to repeat the code, so I created a function (below).

Without a php function, I would do the following in a script

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

Now I know how to do the rest, but I'm stuck on str_replace() as shown above. I have several str_replace() functions to replace placeholders in an email template. I would like to add the str_replace() function to my function (see below) and get it to find all the instances [\] in the email template that I give it and replace it with placeholder values ​​that I will give you as follows: str_replace("[\]", 'replace_with', $email_body)

The problem is that I don’t know how to pass multiple placeholders and their replacement values ​​to my function and get str_replace("[{\}]", 'replace_with', $email_body) to handle all the placeholders that I give them, and replace them with the appropriate values.

Since I want to use this function in several places and to avoid code duplication, in some scripts I can pass the function 5 placeholders and there are values, and another script may need to pass 10 placeholders and use the values ​​for the function in the email template there .

I'm not sure if I will need to use an array in script (s) that will use the function and the for loop in the function, perhaps to force my php function to accept xx placeholders and xx values ​​from the script, and to scroll the placeholders and replace their values ​​there.

Here is my function, which I mentioned above. I commented on a script that might explain a lot easier.

 // 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->IsSMTP(); // enable smtp authentication $mail->SMTPAuth = SMTP_AUTH; // host of the smtp server $mail->Host = SMTP_HOST; // port of the smtp server $mail->Port = SMTP_PORT; // smtp user name $mail->Username = SMTP_USER; // smtp user password $mail->Password = SMTP_PASS; // mail charset $mail->CharSet = MAIL_CHARSET; // set from email address $mail->SetFrom(FROM_EMAIL); // to address $mail->AddAddress($to_email); // email subject $mail->Subject = $email_subject; // html message body $mail->MsgHTML($email_body); // plain text message body (no html) $mail->AltBody(strip_tags($email_body)); // finally send the mail if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message sent Successfully!"; } } 
+3
source share
3 answers

Simple, see strtr & shy; Docs :

 $vars = array( "[{USERNAME}]" => $username, "[{EMAIL}]" => $email, ); $message = strtr($message, $vars); 

Add as many (or as few) pairs of substitutions as you want. But I suggest that you process the template before you call the phpmailer function, so everything remains aside: templating and sending mail:

 class MessageTemplateFile { /** * @var string */ private $file; /** * @var string[] varname => string value */ private $vars; public function __construct($file, array $vars = array()) { $this->file = (string)$file; $this->setVars($vars); } public function setVars(array $vars) { $this->vars = $vars; } public function getTemplateText() { return file_get_contents($this->file); } public function __toString() { return strtr($this->getTemplateText(), $this->getReplacementPairs()); } private function getReplacementPairs() { $pairs = array(); foreach ($this->vars as $name => $value) { $key = sprintf('[{%s}]', strtoupper($name)); $pairs[$key] = (string)$value; } return $pairs; } } 

Use can be greatly simplified, and you can pass the entire template to any function that requires a line input.

 $vars = compact('username', 'message'); $message = new MessageTemplateFile('email.tpl', $vars); 
+14
source

PHP solutions can be:

Find a broad answer at Programmers.StackExchange to learn other approaches to the PHP email template .

+1
source

Why don't you just create an email template for your php file? Then you can do something like:

 Hello <?=$name?>, my name is <?=$your_name?>, today is <?=$date?> 

inside the email to generate your HTML code and then send the result as an email.

I think I like it when you get around it with difficulty?

0
source

Source: https://habr.com/ru/post/1379310/


All Articles