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
複製代碼 代碼如下:
<?php
header('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,即可以示範。具體代碼根據實際需求進行修改。