As you have not mentioned where the variables come from, I'll give you a general example of a templates system:
DB Information:
<html>
Nome: $nome$
</html>
PHP
<?php
$contrato = /* aqui você pega do DB os dados */;
$nome = 'José Maria'.
$idade = '17';
$contrato = str_replace( '$nome$', $nome, $contrato );
$contrato = str_replace( '$idade$', $idade , $contrato );
... faça o mesmo para todos os campos ...
echo $contrato;
?>
Note that in DB you can use a markup that does not confuse, such as $nome$
instead of $nome
, in order to be unambiguous if there is something like $nomenclatura
(which starts with $nome
too).
If you already have a lot in the DB, you should at least be careful, with words started in the same way, to exchange the longest ones first in the sequence of replaces (change $nomenclatura
before $nome
, otherwise the $nome
will move in something that should not).
View the syntax of str_replace
using array to make several substitutions at once:
$contrato = str_replace(
array( '$nome$', '$idade$', '$endereco$' ), // Palavras a trocar
array( $nome , $idade , $endereco ), // O que vai por no lugar de cada uma
$contrato
)
The following question can also help:
How to create a function to page dynamically created PHP page and change certain text