Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php从模板自动生成页面_Php_File_Templates - Fatal编程技术网

php从模板自动生成页面

php从模板自动生成页面,php,file,templates,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."); f

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

<?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!";
 ?>

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

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

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