How to include a PHP variable inside text saved in MySQl

0

I am trying to insert PHP variables into a text that is saved in my MySql, so that they are automatically read in my application. First, let me explain what my reality is:

Print page:

<?php
//PRIMEIRO, FAÇA A BUSCA POR ALGUNS VALORES DO BD.
//EXEMPLO: $variavel1, $variavel2, etc...
?>

//TEXTO PADRÃO EM HTML (para impressão):
// PERCEBAM QUE INCLUO AS VARIAVEIS PHP DENTRO DO TEXTO
Número do contrato: <?php echo $variavel1;?><br>
Nome: <?php echo $variavel2;?><br>
Endereço: <?php echo $variavel3;?>
//AQUI VAI O RESTANTE DO TEXTO DO CONTRATO ....

Instead of keeping this text static, so I only change the value of the variables, I would like to register it also in the bd, so that it is easier for me to change it. It looks like this:

//TEXTO SALVO NO BD:
Número do contrato: {contrato}
Nome: {nome}
etc...

DAI, when I run the application, it already loads the data automatically.

In this way, I could both change the text as well as the place variables. Is it possible?

    
asked by anonymous 18.05.2017 / 17:39

1 answer

1

This is a template.

Just replace the "tags" with the variables.

Example:

$texto_db = 'Número do contrato: {contrato}
Nome: {nome}
etc...';

echo str_replace(
    array(
        '{contrato}',
        '{nome}'),
    array(
        $variavel1,
        $variavel2),
    $texto_db
);
    
18.05.2017 / 17:47