Home > php教程 > php手册 > body text

PHP 动态生成静态HTML页面示例代码

WBOY
Release: 2016-06-06 20:25:37
Original
772 people have browsed it

这篇文章主要为大家分享下PHP 动态生成静态HTML页面示例代码,需要的朋友可以参考下

temp.html

复制代码 代码如下:






{penglig_site_title}





test.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,即可以演示。具体代码根据实际需求进行修改。

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!