Interpolation works when the string is handled directly by the PHP interpreter. Since it is static in the code, it can analyze it and generate the necessary structure for the interpolation of information.
When a string is stored in the database, it ceases to be evaluated directly by the interpreter for security reasons. Since the content comes from external sources, it is understood that the developer will not have full control over the content and therefore it was decided not to evaluate it to avoid possible security loopholes.
A very sensitive way to resolve yourself is to indicate to the interpreter that he should evaluate the string that way and this is done through the function eval
. With the eval
you assume that the data source is secure and that evaluate the interpolation of the string will not expose any sensitive application data.
Considering
// Texto vindo do banco de dados:
$smsQuery->message = "Você Possui Fatura em Aberto. Fatura: {$destinatario->identificador}";
$destinatario->identificador = "687918";
In performing eval($smsQuery->message)
to string would be evaluated by interpolation of values.
Another way is to do the translation of your characters string. Instead of defining, for example:
"Você Possui Fatura em Aberto. Fatura: {$destinatario->identificador}"
You can do
"Você Possui Fatura em Aberto. Fatura: {{identificador}}"
And in PHP do the proper translation of the characters:
$dados = ['{{identificador}}' => $destinatario->identificador];
echo strtr($smsQuery->message, $dados);
Thus, you, as a developer, will have full control over the data exposed through the array $dados
. Values not specified therein array shall not be altered and shall remain in string final.
See working on Ideone.
Other option:
sprintf
.– bfavaretto
@bfavaretto I don’t know if
sprintf
will fit in this case, because as the number of variables in the template may vary, you will not know which values to replace. The template would look like"Olá %s"
and we wouldn’t know which variable to replace.– Woss
Yeah, with
sprintf
It can’t be as automatic, but it’s a kind of consolidated option for storing templates. But I agree it’s bad to lose the semantics of variables.– bfavaretto
Using strtr worked perfectly, but now I will have to develop something to recover all the names of the query fields, to add in the $data matrix, as they will be the variables available to use within $smsQuery->message
– Raphael Godoi