0

I have an email template which i am saving in database. My problem is some part of message are variable means these data are coming from current user data. For Example My Template is

$message="This is test for $username. I am sending mail to $email."

here $username and $email is coming from current users and it is varying from user to user. So problem is how to save it in database so i can use it as variable on php page later. anybody have any idea please help me.your help would be appreciated.

4
  • What are you trying to save? The string with the placeholders or the resulting string after it's been populated with values? Either way, you'd write it to the database as any other string value. Commented Sep 12, 2014 at 12:43
  • 3
    instead of $ you can switch to { for example {username}, then you can use str_replace in future for any substitution of variable Commented Sep 12, 2014 at 12:44
  • i want to save string with placeholders. Commented Sep 12, 2014 at 12:47
  • 1
    @Pundit has a good example, but this can be done the way you are doing it with eval. For example, if you want to print the string containing $username and you want the variable value of $username to replace the variable name in the string, you use: eval("print \"$yourstring\";"); But (and this is a huge BUT), you should never use eval with user-supplied data. That is why I like Pundit's solution as it doesn't eval possible malicious code. Commented Sep 12, 2014 at 12:54

3 Answers 3

3

If you really need to store whole template in database, you can save it using your own created constants e.g. [USERNAME], [EMAIL] and then in php script just use str_replace() on them.

$messageTemplate = 'This is test for [USERNAME]. I am sending mail to [EMAIL].';
$message = str_replace(array('[USERNAME]', '[EMAIL]'), array($username, $email), $messageTemplate);

But you can also divide this string and concatenate it with variables from database as follows:

$message = 'This is test for ' . $username . '. I am sending mail to ' . $email . '.';
Sign up to request clarification or add additional context in comments.

Comments

2

You can use something like this:

$input = "This is test for {username}. I am sending mail to {email}.";

$tokens = array("username" => $username, "email" => $email);

$tmp = $input;
foreach($tokens as $key => $token)
{
    $tmp = str_replace("{".$key."}", $token, $tmp);
}
echo $tmp;

Comments

1

The variables in the string will not be evaluated as variables automatically just because you are adding it to your php scope. You need to eval the string in order for the variables to be replaced:

$username = 'test';
$email = '[email protected]';
$str = "This is a test for $username. I am sending mail to some person $email.";

echo $str. "\n";
// This is a test for $username. I am sending mail to some person $email.
eval("\$str = \"$str\";");
echo $str. "\n";
// This is a test for test. I am sending mail to some person [email protected].

For more information, see http://php.net/manual/en/function.eval.php

10 Comments

Variables set inside single quotes will not be interpolated unless concatenated. $str = 'This is test for $username. I am sending mail to $email.'; will fail. $str = "This is test for $username. I am sending mail to $email.";
and please note while using eval, that someone could inject bad code.
@Fred-ii- thanks fred, btw, could you tell how come the example used single quotes if that wouldn't work? i'm curious.
You're welcome. As previously noted, using eval() has security-related issues and should be used with the utmost of care. Raphael made a point about that, therefore and in the OP's case, eval() is not needed to use. A simple replacement of single quotes to doubles and removing eval() will be much safer to use.
@Fred-ii- you got everyone confused. Vinie, this only seems to work, but it doesn't serve the purpose you need. The interpolation takes place at the first definition of the string (in your case this would be before inserting into the db) not at the eval (in your case after getting the template from db), the first definition of the string should not have double quotes. ideone.com/K30Fjg - both printed strings contain the interpolated variables - first print should not contain the interpolated variables - like this: ideone.com/vh8Dkk (which is the initial proposed solution)
|

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.