php动态网页转换成html,怎么把动态的php文件转换成静态的html文件,html文件是php文件…...
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
生成靜態(tài)文件除了2樓所說的ob緩存方法之外,還有一種方法就是通過使用模板替換技術(shù)實(shí)現(xiàn)(正則表達(dá)式)
header("content-type:text/html;charset=utf-8");
//處理添加、修改、刪除請求
//1.接收一下oper
$oper=$_REQUEST['oper'];
if($oper=="add"){
//接收title,content
$title=$_POST['title'];
$content=$_POST['content'];
//1.把數(shù)據(jù)放入到mysql, 同時創(chuàng)建一個html
//添加到數(shù)據(jù)庫 SqlHelper.class.php
$conn=mysql_connect("localhost","root","1234abcd");
if(!$conn){
die("連接失敗");
}
mysql_select_db("news",$conn);
mysql_query('set names utf8');
$sql="insert into news (title,content) values('$title','$content')";
if(mysql_query($sql,$conn)){
//獲取剛剛插入數(shù)據(jù)的id號
$id=mysql_insert_id();
$html_filename="news_id".$id.".html";
//echo "文件名=".$html_filename;
//創(chuàng)建html文件
$fp=fopen("news.tpl","r");
$html_fp=fopen($html_filename,"w");
//思路->tmp->html 逐行讀取template.tpl文件,然后逐行替換
while(!feof($fp)){
//讀取一行.
$row=fgets($fp);
//含義是 用 $title的內(nèi)容替換 $row中的 %title%
$row=str_replace("%title%",$title,$row);
$row=str_replace("%content%",$content,$row);
//把替換后的一行寫入到html文件
fwrite($html_fp,$row);
}
//關(guān)閉文件流
fclose($fp);
fclose($html_fp);
echo "添加到數(shù)據(jù)庫并成功創(chuàng)建html文件返回列表";
}else{
die('添加失敗');
}
mysql_close($conn);
}
代碼來源:韓順平php系列視頻教程之 頁面靜態(tài)化技術(shù) 培訓(xùn)筆記
總結(jié)
以上是生活随笔為你收集整理的php动态网页转换成html,怎么把动态的php文件转换成静态的html文件,html文件是php文件…...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 语法 条件变量,C ++核心准则
- 下一篇: oracle中判断是否为季末,Oracl