PHP動態網頁產生靜態頁的3種常用方法,php動態靜態3種_PHP教程

來源:互聯網
上載者:User

PHP動態網頁產生靜態頁的3種常用方法,php動態靜態3種


產生靜態頁的頁面非常的簡單就是定義好模板與模板標題,之後利用str_replace進行替換了,是最常用的方法,另一種是利用ob_get_contents輸出獲得然後產生html,還有一種不怎麼推薦的是使用file_get_contents直接執行訪問遠程檔案然後進行儲存,效能極差。

具體方法簡單說明如下:
1.使用檔案函數得到靜態頁面的模板字串,然後用str_replace函數將需要替換的東西替換了再寫入到新的檔案中。
2. 利用PHP的輸出控制函數(Output Control)得到靜態頁面字串,再寫入到新的檔案中。
複製代碼 代碼如下:$filemodel="template/it.php"; //模板地址
$file=fopen($filemodel,"rb"); //開啟模板,得到檔案指標
$temp=fread($file,filesize($filemodel)); //得到模板檔案html代碼

方法一:ob_get_contents()

這是一種很方便的方法,也是很常用的方法,實現原理是:首先開啟緩衝,然後建立相應的靜態頁檔案,寫入緩衝的內容,清空緩衝。
樣本:
複製代碼 代碼如下:ob_strart();//開啟緩衝區
$fn=date('ymdhis').rand(1000,9999).'html';//組建檔案名
require("supply.php");//載入要產生靜態頁的檔案,因為後台有ob_clean()所以在不會顯示出來
$fs=fopen($fn,'w');//開啟靜態頁檔案
fwrite($fs,ob_get_contents());//產生靜態檔案
ob_clean();//清空緩衝

方法二:file_get_contents();
複製代碼 代碼如下:$fn=date('ymdhis').rand(1000,9999).'html';
$url= 'http://'.$_SERVER['HTTP_HOST']."/";//注意
$content=file_get_contents($url);
$fs=fopen($fn,'w');
fwrite($fs,$content);
下面對上面的注意進行一下解釋,如果在些你使用的是僅僅是檔案名稱,而不是URL那麼您這個檔案中如果有使用引用檔案比如require ('header.php'); 那麼header.php中的內容將會顯示不出來。

方法三:str_replace()
複製代碼 代碼如下:$filemodel="supply.php"; 字串5$file=fopen($filemodel,"w+");
$temp=fread($file,filesize($filemodel));
$temp=str_replace("[title]",$title,$temp);
$temp=str_replace("[postTime]",$postTime,$temp);
$temp=str_replace("[content]",$content,$temp);
該方法適用於很簡單的頁面,如果supply.php中有使用引用檔案比如require ('header.php');那麼header.php中的內容將會顯示不出來
在實際應用中,您可以寫一個產生靜態頁的類,
複製代碼 代碼如下:/*+++
|
| 使用方法
| $shtml = new Shtml($Url,$FileBag,$FolderName,$fileid)
| $Url: 頁面 URL 地址
| $FileBag: 檔案夾標記 1 為:指定檔案夾
| 2 為:預設資料夾(時間(年月日))
| $FolderRoot html檔案存放路徑
| $FolderName 指定檔案夾的名稱 $FileBag為2時 可以寫為空白("");
| $fileid 靜態頁面名稱(尾碼 預設為 .html)
|
|
|
/*++*/
class Shtml
{
var $message1="Error 1: You write class Shtml is Wrong ! The second parameter is 1 or 2 in this class!.";
var $message2="Error 2: The file write Error.";
function __construct ($Url,$FileBag,$FolderRoot,$FolderName,$fileid)
{
$this->Url = $Url;
$this->FileBag = $FileBag;
$this->FileRoot = $FolderRoot;
$this->FileName = $FolderName;
$this->fileid = $fileid;
Shtml::useFolder ();
}
/*************擷取資料*******************/
public function loadcontent ($Folder)
{
ob_start();
require_once $this->Url;
Shtml::writehtml ($Folder,ob_get_contents());
ob_clean();
}
/********** 指定檔案夾*****************/
public function useFolder ()
{
if($this->FileBag==1)
{
$Folder=$this->FileName;
}
else if($this->FileBag==2)
{
$Folder=date('Ymd',time());
}
else
{
exit($this->message1);
}
if(!is_dir($this->FileRoot.$Folder)){ mkdir($this->FileRoot.$Folder,0700);}
Shtml::loadcontent ($Folder);
}
/********** 產生靜態頁面*****************/
public function writehtml ($Folder,$cache_value)
{
$file = fopen($this->FileRoot.$Folder.'/'.$this->fileid.'.html','w+');
fwrite($file,$cache_value);
fclose($file);
}
}
$fileid=2;
$shtml = new Shtml("http://www.bkjia.com",1,"","cc",$fileid);
總結, 這個產生html程式碼沒有產生分頁了,如果文章很多它也只產生一篇文章,如果要改進我們需要進行比較大的改動了,在此就不介紹了。感興趣的朋友可以動手測試改進一下,相信會有不小的收穫!

希望本文所述對大家的PHP程式設計有所協助。

http://www.bkjia.com/PHPjc/910598.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/910598.htmlTechArticlePHP動態網頁產生靜態頁的3種常用方法,php動態靜態3種 產生靜態頁的頁面非常的簡單就是定義好模板與模板標題,之後利用str_replace進行替換...

  • 聯繫我們

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