首页 > 开发 > PHP > 正文

php生成静态页面代码

2024-05-04 21:48:56
字体:
来源:转载
供稿:网友

本款生成静态页面程序实现原理是做好自定的模板标签,然后由str_replace把标签替换成指定的内容,再由fopen生成指定 文件名的静态页面,这样就OK了,代码如下:

  1. header('content-type:text/html;charset=utf-8'); 
  2. if(!function_exists('file_get_contents')){ //如果系统没有file_get_contents()函数 
  3.  function file_get_contents($file){ //自己写file_get_contents()函数 
  4.   $fp = fopen($file,'r'); 
  5.   $content = fread($fp,filesize($file)); 
  6.   fclose($fp); 
  7.   return $content
  8.  } 
  9. $tmp_file = 'template.html'//模板文件 
  10. $content = file_get_contents($tmp_file); //获得模板文件内容 
  11. $title = 'title'//模板变量title要替换的值 
  12. $text = 'text'//模板变量text要替换的值 
  13. $content = str_replace('<{title}>',$title,$content); //替换模板变量title 
  14. $content = str_replace('<{text}>',$text,$content); //替换模板变量text 
  15. //echo $content; //显示替换后的模板文件内容 
  16. makehtml('news.html',$content);//写入生成后的静态文件内容到news.html文件 
  17. echo '<a href="news.html" target="_blank">查看文件</a>'
  18. function makehtml($file,$content){ 
  19. //开源代码Vevb.com 
  20.  $fp = fopen($file,'w'); 
  21.  fwrite($fp,$content); 
  22.  fclose($fp); 

template.html,代码如下:

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表