行業(yè)動態(tài)
php生成HTML的兩種方法、PHP生成靜態(tài)頁的兩種方法
發(fā)布日期:2013-01-16 閱讀次數(shù):3742 字體大?。?a href="javascript:;" onclick="ChangeFontSize('content',16)">大

方法一,將本機(jī)或所在虛擬主機(jī)(站點(diǎn))的某個頁面生成HTML 引用時必段使用相對路徑,復(fù)制以下代碼,保存為yiskyphp.php,然后訪問url+yiskyphp.php如http://www.002bubu.com/yiskyphp.php

PHP代碼
  1. <?php    
  2. $nowtime=time();    
  3. $pastsec = $nowtime - $_GET["t"];    
  4.   
  5. if($pastsec<60)    
  6. {    
  7. exit//1分鐘更新一次,時間可以自己調(diào)整    
  8. }    
  9.   
  10. ob_start(); //打開緩沖區(qū)    
  11. include("phpinfo.php");    
  12. $content = ob_get_contents(); //得到緩沖區(qū)的內(nèi)容    
  13. $content .= "<script language=javascript src='yiskyphp.php?t=".$nowtime."'></script>";   
  14.   
  15. file_put_contents("index.html",$content);    
  16.   
  17. if (!function_exists("file_put_contents"))    
  18. {    
  19. function file_put_contents($fn,$fs)    
  20. {    
  21. $fp=fopen($fn,"w+");    
  22. fputs($fp,$fs);    
  23. fclose($fp);    
  24. }    
  25. }    
  26.   
  27. ?>  

方法二:將指定網(wǎng)頁中的內(nèi)容生成HTML,引用的地址需為直接地址。將文件保存為:php2html.php

PHP代碼
  1. <?php    
  2. $nowtime=time();    
  3. $pastsec = $nowtime - $_GET["t"];    
  4.   
  5. if($pastsec<60)    
  6. {    
  7. exit//1分鐘更新一次,時間可以自己調(diào)整    
  8. }    
  9.   
  10. $content = file_get_contents('http://www.002bubu.com/index.asp');//也可引用擴(kuò)展名為PHP的網(wǎng)頁    
  11. $content=preg_replace("/[\r\n]+/"''$content ); //是將回車去掉,此段可有可無,如影響使用請去除  
  12.   
  13. $content .= "<div style='display:none'>請注意此行代碼不要刪除 此為定時生成的必要代碼 亳州易天科技 WWW.002bubu.com PHP自動生成HTML(PHP生成靜態(tài)面頁)實(shí)例</div><script language=javascript src='php2html.php?t=".$nowtime."'></script>";   
  14.   
  15. file_put_contents("index.html",$content);    
  16.   
  17. if (!function_exists("file_put_contents"))    
  18. {    
  19. function file_put_contents($fn,$fs)    
  20. {    
  21. $fp=fopen($fn,"w+");    
  22. fputs($fp,$fs);    
  23. fclose($fp);    
  24. }    
  25. }    
  26.   
  27. ?>