Home  >  Article  >  Backend Development  >  php使用标签替换的方式生成静态页面_PHP教程

php使用标签替换的方式生成静态页面_PHP教程

WBOY
WBOYOriginal
2016-07-13 09:53:20679browse

php使用标签替换的方式生成静态页面

   php可以通过自带函数preg_replace可以用数组批量替换,不过用正则表达式替换效率很低,用起来也不方便。具体参考php手册。有需要的小伙伴可以参考下。

  这段代码演示了php如何通过自定义的模板页面和自定义标签生成静态页面。原理非常简单,就是将模板页面中的标签替换成动态数据即可。希望能给你一定的启发。

  template.html 模板文件

  ?

1

2

3

4

5

6

7

8

9

10

{site_title} - sharejs.com

  test.php 动态文件

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

header('content-type:text/html; charset=utf-8');//防止生成的页面乱码

$title = "PHP 动态生成静态HTML页面_脚本分享网"; //定义变量

$url = "http://www.sharejs.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>";

?>

  以上所述就是本文的全部内容了,希望大家能够喜欢。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1003823.htmlTechArticlephp使用标签替换的方式生成静态页面 php可以通过自带函数preg_replace可以用数组批量替换,不过用正则表达式替换效率很低,用起来也不方便...
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