思路
用fopen函数和fread函数得到模板,然后用str_replace函数替换模板标签为变量,最后用fwrite函数输出新的HTML页面
index.html模板页面
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 6 <title>{title}</title> 7 <meta name="description" content=""> 8 <meta name="keywords" content=""> 9 <link href="" rel="stylesheet"> 10 </head> 11 <body> 12 文章内容为:{content} 13 </body> 14 </html>
index.php
1 <?php 2 header("Content-type: text/html; charset=UTF-8"); 3 $conn=mysql_connect(‘localhost‘,‘root‘,‘‘); 4 $db=mysql_select_db(‘minda‘,$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 ?>
原文:http://www.cnblogs.com/xs-yqz/p/4854618.html