Home  >  Article  >  php教程  >  PHP dynamically generates static HTML page sample code

PHP dynamically generates static HTML page sample code

高洛峰
高洛峰Original
2016-12-21 11:21:422268browse

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(&#39;content-type:text/html; charset=utf-8&#39;);//防止生成的页面乱码
$title = "PHP 动态生成静态HTML页面_脚本之家"; //定义变量
$url = "http://www.php.cn/";
$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(&#39;生成成功&#39;);window.location.href=&#39;".$dest_file."&#39;;</script>";
?>

Run test.php to demonstrate. The specific code will be modified according to actual needs.

For more articles related to PHP dynamically generating static HTML page sample code, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Apache URL rewritingNext article:Apache URL rewriting