思路
用fopen函數和fread函數得到模板,然後用str_replace函數替換模板標簽為變量,最後用fwrite函數輸出新的HTML頁面
index.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>{title}</title> </head> <body> 文章內容為:{content} </body> </html> index.php
<? header('Content-Type:text/html; charset=utf-8'); $conn=mysql_connect('localhost','root',''); $db=mysql_select_db('bbs',$conn); mysql_query('set names utf8'); $sql="select * from notice"; $query=mysql_query($sql); //print_r($arr); while($arr=mysql_fetch_array($query)) { $title=$arr[title]; $content=$arr[content]; $file="index.html"; $neirong=$arr[id].".html"; $fp=fopen($file,'r'); $ht=fread($fp,filesize($file)); $str=str_replace('{title}',$title,$ht); $str=str_replace('{content}',$content,$str); fclose($file); $file=fopen($neirong,'w'); $write=fwrite($file,$str); } ?>
作者“PHP學習筆記”