现在的位置: 首页 > 综合 > 正文

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

2013年12月10日 ⁄ 综合 ⁄ 共 1654字 ⁄ 字号 评论关闭
一个简单的实例:
新闻模版文件news_tmp.html:
<html>
<head>
<title>{title}</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<TABLE border=0 width=767 cellspacing="0" cellpadding="5">
  <TR>
 <TD>
      <div align="center">{news_title}</div>
    </TD>
</TR>
<TR>
 <TD>
      <div align="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") 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<count($image_path);$n++)
  $news_image.="<img src=".$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 = 'webjx';
  $hello = 'webjxcom!';
  $file = file_get_contents('templets.htm');
  $file = str_replace(array('{title}','{hello}'),array($title,$hello), $file);
  echo $file;

?>

抱歉!评论已关闭.