php自动从模板生成页面

时间:2017-10-09 08:15:29

标签: php file templates

我正在开发一个项目,我希望保存修改并将模板保存在基于用户填写表单的名称中。我找到了一种保存文件的方法,但我想使用模板并根据用户填写的表单修改该模板中的值

<?php
    $name='kavin';
    $fh = fopen($name + ".html", 'w') or die("Could not create the file.");
    fwrite($fh, $text) or die("Could not write to the file.");
    fclose($fh);
    echo "File " . $name . ".html created!";
 ?>

1 个答案:

答案 0 :(得分:0)

制作模板,使用 {username} 作为模板中值的占位符。

$name = 'kavin';
$template = file_get_contents('path/to/my/template.php');
$template = str_replace('{username}', $name, $template);

file_put_contents($name . '.html', $template);