PHP动态页面 生产静态页 方法二

PHP文件:1.php

<?php
header('Content-type: text/html; charset=utf8');
$title = "这个是标题吗??变量名title";
$file = "这是什么?这个变量名为file";
$fp = fopen ("templets/temp.html","r");
$content = fread ($fp,filesize ("templets/temp.html"));
$content = str_replace ("{file}",$file,$content);
$content = str_replace ("{title}",$title,$content);

/* // 生成列表开始
$list = '';
$sql = "select id,title,filename from article";
$query = mysql_query ($sql);
while ($result = mysql_fetch_array ($query)){
    $list .= '<a href='.$root.$result['filename'].' target=_blank>'.$result['title'].'</a><br>'; }
    $content .= str_replace ("{articletable}",$list,$content); //生成列表结束
    // echo $content; */

$filename = "test/test.html";
$handle = fopen ($filename,"w"); //打开文件指针,创建文件
if (!is_writable ($filename)) {
    die ("文件:".$filename."不可写,请检查其属性后重试!");
}
if (!fwrite ($handle,$content)){ //将信息写入文件
    die ("生成文件".$filename."失败!");
}
fclose ($handle); //关闭指针
die ("创建文件".$filename."成功!");
?>

模板文件名:templets/temp.html

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

</body>
<table width="500" border="0" cellpadding="5" cellspacing="1" align="center" bgcolor="#add3ef">
  <tr bgcolor="#eff3ff">
    <td>{file}</td>
  </tr>
  <tr bgcolor="#fff">
    <td>{title}</td>
  </tr>
</table>
</form>
</html>
原文地址:https://www.cnblogs.com/xcxc/p/2572681.html