Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)归海一刀
published in(发表于) 2014/2/17 7:38:29
php生成静态页面的简单实例_[PHP教程]

php生成静态页面的简单实例_[PHP教程]

一个简单的实例:
新闻模版文件news_tmp.html:


{title}






{news_title}





{news_image}





{news_content}




______________________________________________________________________
答7:
新闻生成文件aaa.php:
//假设下面信息都来自表单
title="娱乐新闻---郑秀文准备宣布退出娱乐圈";
news_title="郑秀文准备宣布退出娱乐圈";
image_path=array("image/xxx.jpg","image/xxx2.jpg","image/xxx3.jpg");
news_content="事业如日方中的郑秀文(Sammi),除是乐坛天后外,亦以五百五十万片酬登上全港片酬最高女星之位......";
fp=@fopen("news_tmp.html","r") or die("没有模版文件或者没有相关权限!");

str=fread(fp,filesize("news_tmp.html"));
fclose(fp);


____________________________________________________________________
答8:
news_filename=time().".html"; //生成的新闻文件名
str=str_replace("{title}",title,str);
str=str_replace("{news_title}",news_title,str);
str=str_replace("{news_content}",news_content,str);
if(count(image_path)){
for(n=0;n news_image.="
";
str=str_replace("{news_image}",news_image,str);
}else
str=str_replace("{news_image}","",str);
fw=fopen(news_filename,"w");
fwrite(fw,str);
?>



简单演示PHP如何使用模板生成静态页面。

模板文件templets.htm:



{title}


Hello {hello}





PHP文件代码:


title = 'dwww';
hello = 'dwwwcn!';
file = file_get_contents(’templets.htm’);
file = str_replace(array(’{title}’,’{hello}’),array(title,hello), file);
echo file;

?>







If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759