思路

用fopen函数和fread函数得到模板,然后用str_replace函数替换模板标签为变量,最后用fwrite函数输出新的HTML页面

index.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>{title}</title> 
  6. </head> 
  7.  
  8. <body> 
  9. 文章内容为:{content}  
  10. </body> 
  11. </html> 

index.php

 
  
  1. <?  
  2. header('Content-Type:text/html; charset=utf-8');  
  3. $conn=mysql_connect('localhost','root','');  
  4. $db=mysql_select_db('bbs',$conn);  
  5. mysql_query('set names utf8');  
  6. $sql="select * from notice";  
  7. $query=mysql_query($sql);  
  8.  
  9. //print_r($arr);  
  10. while($arr=mysql_fetch_array($query))  
  11.  {  
  12.      $title=$arr[title];  
  13.      $content=$arr[content];  
  14.      $file="index.html";  
  15.      $neirong=$arr[id].".html";  
  16. $fp=fopen($file,'r');  
  17. $ht=fread($fp,filesize($file));  
  18. $str=str_replace('{title}',$title,$ht);  
  19. $str=str_replace('{content}',$content,$str);  
  20. fclose($file);  
  21. $file=fopen($neirong,'w');  
  22. $write=fwrite($file,$str);  
  23.  }  
  24.  
  25. ?>