這篇文章主要為大家分享下PHP 動態產生靜態HTML頁面範例程式碼,需要的朋友可以參考下
temp.html 代碼如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>{penglig_site_title}</title></head><body><iframe width="100%" height="1000px" scrolling="yes" frameborder="0" src="{penglig_site_url}" ></iframe></body></html> test.php 代碼如下:<?phpheader('content-type:text/html; charset=utf-8');//防止產生的頁面亂碼$title = "PHP 動態產生靜態HTML頁面_指令碼之家"; //定義變數$url = "http://www.jb51.net/";$temp_file = "temp.html"; //臨時檔案,也可以是模板檔案$dest_file = "dest_page.html"; //產生的目標頁面 $fp = fopen($temp_file, "r"); //唯讀開啟模板$str = fread($fp, filesize($temp_file));//讀模數板中內容 $str = str_replace("{penglig_site_title}", $title, $str);//替換內容$str = str_replace("{penglig_site_url}", $url, $str);//替換內容fclose($fp); $handle = fopen($dest_file, "w"); //寫入方式開啟需要寫入的檔案fwrite($handle, $str); //把剛才替換的內容寫進產生的HTML檔案fclose($handle);//關閉開啟的檔案,釋放檔案指標和相關的緩衝區echo "<script>alert('產生成功');window.location.href='".$dest_file."';</script>";?> 運行test.php,即可以示範。具體代碼根據實際需求進行修改。