This phrase seems confusing because you need something seemingly simple, but you want to do it in the "hardest" way.
I'll need this for the automatic generation of simple pages, the complete layout is in the template.php, and the main values of the page are generated dynamically.
If you want to run model.php
, I recommend that you use include
because then it will execute the script called, but I'm not sure what you want, so follow the answer ...
As I said fopen
does not execute php scripts, it reads only, the only way I can see is to use str_replace
or strtr
, for example:
$arq="modelo.php";
$abre=fopen($arq, "r+");
$conteudo = fread($abre, filesize($arq));
$conteudo = str_replace('$variavel', $variavel, $conteudo);
fclose($abre);
echo $conteudo;
Note that r+
opens the file to edit (put the pointer at the end) and read, in your case it seems that you just want to read, then you can use file_get_contents
, for example:
$arq = 'modelo.php';
$conteudo = file_get_contents($arq);
$conteudo = str_replace('$variavel', $variavel, $conteudo);
echo $conteudo;
Note that writing $var
within '...'
(apostrophes) does not execute the variables.
A detail, if there is any variable type $variavel2
this can be a problem, so maybe it is better to use regex, eg:
$conteudo = preg_replace('#\$(variavel)([^a-z0-9]+)#',
'$' . $variavel . '$2', $conteudo);
echo $conteudo;
If you have more variables you can use strtr
, like this:
$arq = 'modelo.php';
$conteudo = file_get_contents($arq);
$trans = array(
'$varA' => $varA,
'$varB' => $varB,
'$varC' => $varC,
'$varD' => $varD,
'$varE' => $varE
);
$conteudo = strtr($conteudo, $trans);