谁能给一个PHP生成静态页面的完整例子???

解决方案 »

  1.   

    收了,我写了一个但是不成功,不知道谁给我改写下:<?php
    /*ob_start():是打开缓冲区的,就是要把您需要生成的静态文件的内容缓存在这里;
    ob_get_contents():是读出缓冲区里的内容,下面有代码为例;
    ob_end_clean():这个比较重要,只有使用了这个函数后,缓冲区里的内容才会读*/
    ob_start(); 
    //readfile("http://localhost/text/index.php");//index.php为模板 
    header("http://localhost/text/index.php");
    $text = ob_get_contents();
    ob_end_clean();
    $myfile = fopen("../index.html","w"); //index.html要生成的静态页面
    //$text = str_replace ("{counent}",$string,$text);
    fwrite($myfile,$text)or die("写文件出错");?>
      

  2.   


    <?php
    $dir=scandir("text");
    if(in_array($name,$dir))
    echo file_get_contents("./text/$name.htm");
    else{
    $text = file_get_contents("./text/$name.$suffix");
    $text = str_replace ("{param}",$string,$text);
    file_put_contents("./templates/$name.htm",$text);
    echo $text;exit;
    }
    ?>
      

  3.   

    我是想一个PHP的页面通过编译之后(在浏览器上看到的东西)换成用html页面显示。这样的话就不用老是去连接数据库,用来减轻服务器的负担。
    请问下高手们,怎么样来写那个方法。
      

  4.   

    <?php
    @session_start();
    $db = mysql_connect(SERVER,USER,PASSWORD) or die("cant't connect host");
    $re = mysql_select_db(DATABASE,$db)or die ("can't open database");$sql = "Select * FROM news";
    $res = mysql_query($sql);
    $row = mysql_num_rows($res);
    $path= date(Ymd);//分页目录"fenye"
    $dir='Article_list';
    $pagesize    = 4;                    //分页行数
    if($row<$pagesize) $pages = 1;        
    if($row%$pagesize){
         $pages   = intval($row/$pagesize)+1;
    }else{
         $pages   = intval($row/$pagesize);
    }for($i=1;$i<=$pages;$i++){
         $page_turn="";
         if($i==1){
             $indexpath="index.html";
             $page_turn.="第一页 | 上一页";
         }else{
             $indexpath="index_".$i.".html";
             $page_turn.="<a href='index.html'>第一页</a> | <a href='index_".($i-1).".html'>上一页</a>";
         }
         if($i==$pages){
             $page_turn.=' | 下一页 | 最后一页';
         }else{
             $page_turn.=" | <a href='index_".($i+1).".html'>下一页</a> | <a href='index_".$pages.".html'>最后一页</a>";
         }
         $search = $sql." LIMIT ".($i-1)*$pagesize .", $pagesize"; 
         $result = mysql_query($search);
         $rows    = mysql_num_rows($result);
         $j=1;
         $list="";
         while($j<=$rows){
             $doc     = mysql_fetch_array($result);
             $id      = $doc['0'];
             $title   = $doc['1'];
             $list    .="<a href='../Article/".$path."/".$id.".html'>".$title."</a><br>";
     
     
     
      //内容生成html------------------------------------
     $newscontent=formathtml($doc['4']);
     $fpshow      = fopen("html/list_show.html","r");
     $strfpshow     = fread($fpshow,filesize("html/list_show.html"));  
     $strfpshow     = str_replace("{newscontent}",$newscontent,$strfpshow);
     fclose($fpshow);
     if(!file_exists("Article/".$path)){mkdir("Article/".$path);}//目录不存在就创建
     $handleshow = fopen("Article/".$path."/".$id.".html","w");//文件写入
     fwrite($handleshow,$strfpshow);
     fclose($handleshow);
     //内容生成html------------------------------------
     
             $j+=1;
         }

     
     //链接调用列表模板
         $list.="<br><br>".$page_turn;
         $fp      = fopen("html/list.html","r");
         $str     = fread($fp,filesize("html/list.html"));  
         $str     = str_replace("{content}",$list,$str);
         fclose($fp);
     //把列表内容给生成静态页面
     if(!file_exists($dir)){mkdir($dir);}//目录不存在就创建
         $handle = fopen($dir."/".$indexpath,"w");//文件写入
         fwrite($handle,$str);
         fclose($handle);
    }       
         copy($dir."/index.html",$dir."/index_1.html");           
    ?> 
    list.html<html ><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    {content}
    </body>
    </html>list_show.html
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    </head><body>
    {newscontent}
    </body>
    </html>
      

  5.   

    我都已经把PHP的页面写好了,我只是想客户端在浏览时不浏览.php的文件,只浏览.html的文件。
      

  6.   

    新建一个comptuer数据库,建立xuehao,uname,name.....一共八个字段。最后在根文件下新建一个room文件夹。就会生成返回数据库中以uname命名的uname.html,有多少列就返回多少个静态页面!<?php
    include("conn.php");
    $sql="SELECT * FROM `comptuer` WHERE 1";
    $query=mysql_query($sql);
    while($row=mysql_fetch_array($query)) { $xuehao=$row[0];
      $uname=$row[1];
      $name=$row[2];
      $age=$row[3];
      $hometown=$row[4];
      $qq=$row[5];
      $simplify=$row[6];
      $description=$row[7];
      $path=$uname.'.htm';
      $fp=fopen("tmp.htm","r"); //只读打开模板
      $str=fread($fp,filesize("tmp.htm"));//读取模板中内容
      $str=str_replace("{xuehao}",$xuehao,$str);
      $str=str_replace("{uname}",$uname,$str);
      $str=str_replace("{name}",$name,$str);//替换内容
      $str=str_replace("{age}",$age,$str);
      $str=str_replace("{hometown}",$hometown,$str);
      $str=str_replace("{qq}",$qq,$str);
      $str=str_replace("{simplify}",$simplify,$str);
      $str=str_replace("{description}",$description,$str);
      fclose($fp);  $handle=fopen("room/$path","w"); //写入方式打开新闻路径
      fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
      fclose($handle);
      echo "生成成功";}
    ?>
      

  7.   

    tmp.htm
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">
    <html>
    <head>
    <meta name="generator" content="HTML Tidy, see www.w3.org">
    <title>ijieoaidjj</title>
    </head>
    <body>
    学号:{xuehao}<br/><hr/>
    英文名:{uname}<br/><hr/>
    中文名:{name}<br/><hr/>
    年龄:{age}<br/><hr/>
    家乡:{hometown}<br/><hr/>
    QQ:{qq}<br/><hr/>
    简介:{simplify}<br/><hr/>
    描述:{description}<br/><hr/>
    </body>
    </html>
      

  8.   

    都不是想要的,我想要的是以.php格式的文件生成.html的文件,现在.php格式的文件都已经做好了,就是把.php格式的文件生成.html的文件怎么处理?
    我的这个方法不能成功,请大家帮我看下哪里错误了<?php
    /*ob_start():是打开缓冲区的,就是要把您需要生成的静态文件的内容缓存在这里;
    ob_get_contents():是读出缓冲区里的内容,下面有代码为例;
    ob_end_clean():这个比较重要,只有使用了这个函数后,缓冲区里的内容才会读*/
    ob_start(); 
    //readfile("http://localhost/text/index.php");//index.php为模板 
    header("http://localhost/text/index.php");
    $text = ob_get_contents();
    ob_end_clean();
    $myfile = fopen("../index.html","w"); //index.html要生成的静态页面
    //$text = str_replace ("{counent}",$string,$text);
    fwrite($myfile,$text)or die("写文件出错");?>