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

时间:2024-05-02 17:18:13 

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 = "https://www.aspxhome.com/";
$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,即可以演示。具体代码根据实际需求进行修改。

标签:PHP,动态,生成静态
0
投稿

猜你喜欢

  • PHP面向对象程序设计之类常量用法实例

    2023-11-22 15:17:39
  • 详解MySQL性能优化(二)

    2024-01-23 13:10:47
  • Python使用5行代码批量做小姐姐的素描图

    2023-09-27 09:53:04
  • Python爬取奶茶店数据分析哪家最好喝以及性价比

    2021-02-19 08:41:45
  • python析构函数用法及注意事项

    2023-01-25 19:53:11
  • Python函数式编程指南(四):生成器详解

    2023-08-23 05:50:02
  • Python opencv缺陷检测的实现及问题解决

    2023-03-10 20:41:55
  • Keras loss函数剖析

    2021-01-24 05:25:27
  • PHP二维码的生成与识别案例

    2023-06-14 06:48:55
  • 解析pandas apply() 函数用法(推荐)

    2022-09-02 15:10:09
  • 详解Python中的正斜杠与反斜杠

    2021-06-28 08:38:10
  • Python3实现的爬虫爬取数据并存入mysql数据库操作示例

    2022-11-06 15:18:59
  • Python中内置数据类型list,tuple,dict,set的区别和用法

    2022-09-10 05:59:46
  • 浅谈Series和DataFrame中的sort_index方法

    2022-07-01 05:53:22
  • Vision Transformer图像分类模型导论

    2022-09-09 08:37:39
  • jquery插件bootstrapValidator表单验证详解

    2024-04-22 22:22:05
  • 古老的问题:清除浮动

    2009-02-12 11:21:00
  • Python如何使用cv2.canny进行图像边缘检测

    2021-03-11 20:38:12
  • 如何使用Docker恢复Mysql8备份的Data数据

    2024-01-14 20:07:04
  • python 实现单通道转3通道

    2021-09-12 22:03:25
  • asp之家 网络编程 m.aspxhome.com