分析PHP產生html檔案的具體程式碼範例_PHP教程

來源:互聯網
上載者:User
學習 PHP產生html檔案1,下面使用模版的一個方法!

  1. < ?php
  2. $fp = fopen ("templets.html","a");
  3. if ($fp){
  4. $fup = fread ($fp,filesize
    ("templets.html"));
  5. $fp2 = fopen ("html.shtml","w");
  6. if ($fwrite ($fp2,$fup)){
  7. $fclose ($fp);
  8. $fcolse ($fp2);
  9. die ("寫入模板成功");
  10. } else {
  11. fclose ($fp);
  12. die ("寫入模板失敗!");
  13. }
  14. }
  15. ?>
 
  1. < ?php
  2. $fp = fopen ("templets.html","a");
  3. if ($fp)
  4. { $fup = fread ($fp,filesize("templets.html"));
  5. $fp2 = fopen ("html.shtml","w");
  6. if ($fwrite ($fp2,$fup))
  7. { $fclose ($fp);
  8. $fcolse ($fp2);
  9. die ("寫入模板成功"); }
  10. else { fclose ($fp);
  11. die ("寫入模板失敗!");
  12. }
  13. }
  14. ?>

簡單的將模板寫進一個檔案中存為html.html

PHP產生html檔案2,按時間產生html檔案名稱

 
  1. < ?php
  2. $content = "這是一個以日期時間為檔案名稱
    的靜態產生網頁的測試檔案,檔案名稱格式一
    般為< font color=#ff0000>年月日時分秒
    .html< /font>";
  3. $datedate = date('YmdHis');
  4. $fp = fopen (date('YmdHis') . '.html',"w");
    //本函數可用來開啟本地或者遠端的檔案 'w'
    開檔案方式為寫入,檔案指標指到開始處,
    並將原檔案的長度設為 0。若檔案不存在,則建立新檔案。
  5. if (fwrite ($fp,$content)){
    //格式是.int fwrite(int fp(檔案名稱),
    string string(內容), int [length]
    (長度));本函數將字串 string 寫入檔案
    資料流的指標 fp 上。若有指定長度 length,
    則會寫入指定長度字串,或是寫到字串結束。
  6. fclose ($fp);//函數用來關閉已經開啟的文
    件的指標 fp。成功返回 true,失敗則返回 false。
  7. die ("寫入模板成功");
  8. }
  9. else {
  10. fclose ($fp);
  11. die ("寫入模板失敗!");
  12. }
  13. echo ($content);
  14. ?>

 
  1. < ?php $content = "這是一個以日期時間為檔案名稱
    的靜態產生網頁的測試檔案,檔案名稱格式一般為
  2. < font color=#ff0000>年月日時分秒.html< /font>";
  3. $datedate = date('YmdHis');
  4. $fp = fopen (date('YmdHis') . '.html',"w");
  5. //本函數可用來開啟本地或者遠端的檔案 'w'
    開檔案方式為寫入,檔案指標指到開始處,
    並將原檔案的長度設為 0。若檔案不存在,則建立新檔案。
  6. if (fwrite ($fp,$content))
  7. {//格式是.int fwrite(int fp(檔案名稱),
    string string(內容), int [length](長度));
  8. 本函數將字串 string 寫入檔案資料流的指標 fp 上。
    若有指定長度 length,則會寫入指定長度字串,
    或是寫到字串結束。 fclose ($fp);
  9. //函數用來關閉已經開啟的檔案的指標 fp。
    成功返回 true,失敗則返回 false。
  10. die ("寫入模板成功"); }
  11. else { fclose ($fp); die ("寫入模板失敗!");
  12. }
  13. echo ($content); ?>

PHP產生html檔案3,下面為轉換檔名的一個方法

 
  1. < ?php
  2. $s_fname = "93e.php";
  3. $o_fname = "93e.htm";
  4. ob_end_clean();
  5. ob_start();
  6. include($s_fname);
  7. $length = ob_get_length();
  8. $buffer = ob_get_contents();
  9. $buffer = eregi_replace("r","",$buffer);
  10. ob_end_clean();
  11. $fp = fopen($o_fname,"w+");
  12. fwrite($fp,$buffer);
  13. fclose($fp);
  14. ?>


 
  1. < ?php
  2. $s_fname = "93e.php";
  3. $o_fname = "93e.htm";
  4. ob_end_clean(); ob_start();
  5. include($s_fname);
  6. $length = ob_get_length();
  7. $buffer = ob_get_contents();
  8. $buffer = eregi_replace("r","",$buffer);
    ob_end_clean();
  9. $fp = fopen($o_fname,"w+");
    fwrite($fp,$buffer); fclose($fp);
  10. ?>

這樣就可以把93e.php轉化為靜態HTML檔案了,要注意的是待轉換的檔案裡不能有,ob_end_clean();和 ob_start();語句,且目錄要有寫入權限。

以上就是PHP產生html檔案的實現方法介紹,僅供大家參考學習。


http://www.bkjia.com/PHPjc/446221.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/446221.htmlTechArticle學習 PHP產生html檔案1,下面使用模版的一個方法! ?php $ fp = fopen (templets.html,a); if($fp){ $ fup = fread ($fp,filesize (templets.html)); $ fp2 = fopen (html.shtml...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.