php的ob實現頁面靜態化

來源:互聯網
上載者:User

標籤:style   blog   color   strong   檔案   資料   ar   art   

php頁面靜態化的原理,用最少的代碼解釋頁面靜態化

如何應用:在插入或更新資料到資料庫時,就執行一下代碼是一種比較好的方法。比如:php執行add()方法時(就是插入資料時)

 1 //開啟緩衝 2 Ob_start(); 3  4 //擷取html模板 5 $tmp_html = file_get_contents(‘template.tpl’); 6 //將模板裡的檔案替換掉 7 $php_content = str_replace(“{”, “<?php echo ”, $tmp_html); 8 $php_content = str_replace(“)”, “?>”, $tmp_html); 9 10 //將新內容輸出到php檔案11 file_put_contents($phpfilename, $php_content);12 13 //包含該檔案時,將會輸出,這是裡面沒有<?php ?>等php的相關字元,但因開啟了ob_start所以會輸出到緩衝中14 Include “$phpfilename”;15 //從緩衝中擷取該頁面的內容,此時的內容沒有<?php ?>等php的相關字元,完全轉為html了16 $html_content = ob_get_contents();17 //將內容輸出到html檔案18 file_put_contents($htmlfilename, $html_content);19 20 //關閉緩衝21 ob_clean();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.