解读php生成静态页面的简单实例

解读php生成静态页面的简单实例

一个简单的实例:

新闻模版文件news_tmp.html:

<html>

<head>

<title>{title}</title>

<metahttp-equiv="Content-Type"content="text/html;charset=gb2312">

</head>

<body>

<TABLE0"cellpadding="5">

<TR>

<TD>

<divalign="center">{news_title}</div>

</TD>

</TR>

<TR>

<TD>

<divalign="center">{news_image}</div>

</TD>

</TR>

<TR>

<TD>{news_content}</TD>

</TR>

</TABLE>

</body>

</html>

______________________________________________________________________

答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")ordie("没有模版文件或者没有相关权限!");

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<count(image_path);n++)

news_image.="<imgsrc=".image_path[n]."><BR>";

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:

<html>

<head>

<title>{title}</title>

</head>

<body>

<p>Hello{hello}</p>

</body>

</html>

PHP文件代码:

<?php

title='dwww';

hello='dwwwcn!';

file=file_get_contents(’templets.htm’);

file=str_replace(array(’{title}’,’{hello}’),array(title,hello),file);

echofile;

?>