PHP生成静态html实例
<?php
require_once("../inc/conn.php");
function readModel($model_path){
global $str_html;
$fp=fopen($model_path,"r"); //只读打开模板
$str_html=fread($fp,filesize($model_path)); //读取模板中内容
fclose($fp);
}
function writeModel($html_path,$str_html){
$handle=fopen($html_path,"w"); //写入方式打开新闻路径
fwrite($handle,$str_html); //把刚才替换的内容写进生成的HTML文件
fclose($handle);
}
$act=$_GET['act'];
$j_title=$_POST['j_title'];
$j_content=$_POST['j_content'];
$j_time=$_POST['j_time'];
$time=$_POST['time'];
if($act=="add"){
if(empty($j_title) || empty($j_content) || empty($j_time) || empty($time)){
echo("请填写完整再提交!<br><br>title:{$j_title}<br>content:{$j_content}<br>time:{$j_time}");
exit;
}
$html_path="../html/{$time}.html"; //生成html文件路径
$model_path="../templets/newsview.html"; //静态模板路径
readModel($model_path); //读取模板
$str_html=str_replace("{j_title}",$j_title,$str_html); //替换内容
$str_html=str_replace("{j_content}",$j_content,$str_html);
$str_html=str_replace("{j_time}",$j_time,$str_html);
writeModel($html_path,$str_html); //写入并生成html文件
echo "success! view:<a href='{$html_path}' target='_blank'>click</a> add:<a href='news.php'>click</a>";
exit;
}
?>
上一篇:PHP读取XML的两种方法
下一篇:PHP排序函数,可对数组进行排序
讨论数量:0