文件一:makehtml.php(PHP生成html静态页面最简单的原理)<?php
//PHP生成静态html页面原理
$fp = fopen ("template.php","r");
$content = fread ($fp,filesize ("template.php"));
$filename="makehtml.html";
$handle = fopen ($filename,"w"); //打开文件指针,创建文件
if (!is_writable ($filename)){
die ("文件:".$filename."不可写,请检查其属性后重试!");
}
if (!fwrite ($handle,$content)){ //将信息写入文件
die ("生成文件".$filename."失败!");
}
fclose ($handle); //关闭指针
die ("创建文件".$filename."成功!");
?>
文件二:template.php(PHP模板替换最简单的原理)<?php
//模板替换原理
$title = '这里是标题';
$hello = '这里是内容!';
$file = file_get_contents('template.html');
$file = str_replace(array('{title}','{hello}'),array($title,$hello), $file);
echo $file;
?>
文件三:template.html(html模板)
{title}{hello}
文件四:makehtml.html(生成的html页面)<?php
//模板原理
$title = '这里是标题';
$hello = '这里是内容!';
$file = file_get_contents('template.html');
$file = str_replace(array('{title}','{hello}'),array($title,$hello), $file);
echo $file;
?>