加入收藏 | 设为首页 | 会员中心 | 我要投稿 汽车网 (https://www.0577qiche.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php产生静态页面代码

发布时间:2023-08-14 11:01:41 所属栏目:PHP教程 来源:
导读:本款生成静态页面程序实现原理是做好自定的模板标签,然后由str_replace把标签替换成指定的内容,再由fopen生成指定 文件名的静态页面,这样就OK了,代码如下:

header('content-type:text/html;charset=utf-8&#39
本款生成静态页面程序实现原理是做好自定的模板标签,然后由str_replace把标签替换成指定的内容,再由fopen生成指定 文件名的静态页面,这样就OK了,代码如下:

header('content-type:text/html;charset=utf-8'); 
if(!function_exists('file_get_contents')){ //如果系统没有file_get_contents()函数 
 function file_get_contents($file){ //自己写file_get_contents()函数 
  $fp = fopen($file,'r'); 
  $content = fread($fp,filesize($file)); 
  fclose($fp); 
  return $content; 
 } 

$tmp_file = 'template.html'; //模板文件 
$content = file_get_contents($tmp_file); //获得模板文件内容 
$title = 'title'; //模板变量title要替换的值 
$text = 'text'; //模板变量text要替换的值 
$content = str_replace('<{title}>',$title,$content); //替换模板变量title 
$content = str_replace('<{text}>',$text,$content); //替换模板变量text 
//echo $content; //显示替换后的模板文件内容 
makehtml('news.html',$content);//写入生成后的静态文件内容到news.html文件 
echo '<a href="news.html" target="_blank">查看文件</a>'; 
function makehtml($file,$content){ 
//开源代码Cuoxin.com 
 $fp = fopen($file,'w'); 
 fwrite($fp,$content); 
 fclose($fp); 

template.html,代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
<title>makehtml</title> 
</head> 
<body> 
这是模板变量title------<{title}> 
<br /> 
这是模板变量text------<{text}> 
</body> 
</html>
 

(编辑:汽车网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章