Replacement of variables in the body of the message

6

I am creating a message profile system, when the system performs a certain action it picks up a certain profile and triggers the message as email , however I would like it in the message body when writing {{variavel}} would like it to take the value of $variavel corresponding (automatically), however I do not know how to call this method and I still have not found any solution, could anyone help?

Explanation :

I have the following message:

  

{{name}} Lorem Ipsum is simply dummy text of the printing and typesetting industry {{email}}

To replace these values I would have to do the following:

str_replace("{{nome}}", $nome, $mensagem);
str_replace("{{email}}", $email, $mensagem);

I just wanted to do this so that it would be automatic.

    
asked by anonymous 16.05.2015 / 21:47

3 answers

3

After viewing the discussion , I came up with the following solutions.

You could use the preg_replace_callback function.

function preg_parse($template, $vars) {
    $re = '/\{\{([^}]+)\}\}/';

    return preg_replace_callback($re, function($match) use($vars) {
        $key = trim($match[1]); //Remove os espaços da direita e esquerda do nome da variável
        return isset($vars[$key]) ? $vars[$key] : $match[0];
    }, $template);
}

Or automate the definition of the variables in the @Sergio solution (which I understood in the discussion was what was missing for you).

function replace_parse($template, $vars) {
    $keys   = array_map(function($key) { return '{{'.$key.'}}'; }, array_keys($vars));
    $values = array_values($vars);

    return str_replace($keys, $values, $template);
}

Taking the following code as an example.

$vars     = array('nome' => 'foo', 'email' => '[email protected]');
$template = '{{nome}} Lorem Ipsum is simply dummy text of the printing and typesetting industry {{email}}';

echo preg_parse($template, $vars).PHP_EOL;
echo replace_parse($template, $vars).PHP_EOL;

The output is.

foo Lorem Ipsum is simply dummy text of the printing and typesetting industry [email protected]
foo Lorem Ipsum is simply dummy text of the printing and typesetting industry [email protected]

But the preg_parse function removes the spaces between the tags {{ and }} , something that replace_parse does not.

    
17.05.2015 / 03:21
3

Another alternative that can be used is the strtr function, it aims to, translate / replace certain characters of a string . Here's an example:

$texto = 'Bem-vindo ao Stack Overflow em Inglês';
$traducao = array('Inglês' => 'Português');

echo strtr($texto, $traducao); // Bem-vindo ao Stack Overflow em Português

View demonstração

Assuming that the nome and email variables are already set to the value you want, do the following:

$mensagem = '{{nome}} Lorem Ipsum is simply text of the printing and typesetting industry {{email}}';
$nome  = 'Maria';
$email = '[email protected]';

$traducao = array("{{nome}}"  => $nome, 
                  "{{email}}" => $email,
                 );

echo strtr($mensagem, $traducao);

View demonstração

    
17.05.2015 / 04:59
1

To replace within a string you can use str_replace(); .

The syntax is:

  

$ stringFinal = str_replace ("to replace", "substitute", "original string");

So in your case it could be something like

$email = str_replace({{variavel}}, $variavel, $corpoDoEmail);

You can also use arrays in str_replace. This way you can replace many pieces at a time:

$pedacos =     array('{{nome}}', '{{mail}}');
$substitutos = array($nome, $email);
$output  =     str_replace($pedacos , $substitutos , $corpoDoEmail);
    
16.05.2015 / 22:02