Home > Backend Development > PHP Tutorial > PHP uses tag replacement to generate static pages_PHP tutorial

PHP uses tag replacement to generate static pages_PHP tutorial

WBOY
Release: 2016-07-13 09:53:20
Original
720 people have browsed it

php uses tag replacement to generate static pages

PHP can use the built-in function preg_replace to batch replace with arrays, but regular expression replacement is inefficient and difficult to use. inconvenient. Please refer to the PHP manual for details. Friends in need can refer to it.

This code demonstrates how PHP generates static pages through customized template pages and custom tags. The principle is very simple, just replace the tags in the template page with dynamic data. Hope it can give you some inspiration.

template.html template file

 ?

1

2

3

4

5

6

7

8

9

10

{site_title} - sharejs.com

1

2

3

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>";

?>

4

5

6

8 9 10
{site_title} - sharejs.com
Test.php dynamic file  ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <🎜>header('content-type:text/html; charset=utf-8');//Prevent the generated page from being garbled<🎜> <🎜>$title = "PHP dynamically generates static HTML pages_script sharing network"; //Define variables<🎜> <🎜>$url = "http://www.sharejs.com";<🎜> <🎜>$temp_file = "temp.html"; //Temporary file, which can also be a template file<🎜> <🎜>$dest_file = "dest_page.html"; //Generated target page<🎜> <🎜>$fp = fopen($temp_file, "r"); //Read-only open template<🎜> <🎜>$str = fread($fp, filesize($temp_file));//Read the content in the template<🎜> <🎜>$str = str_replace("{penglig_site_title}", $title, $str);//Replacement content<🎜> <🎜>$str = str_replace("{penglig_site_url}", $url, $str);//Replacement content<🎜> <🎜>fclose($fp);<🎜> <🎜>$handle = fopen($dest_file, "w"); //Open the file to be written in writing mode<🎜> <🎜>fwrite($handle, $str); //Write the content just replaced into the generated HTML file<🎜> <🎜>fclose($handle);//Close the open file and release the file pointer and related buffer<🎜> <🎜>echo "<script>alert('Generation successful');window.location.href='".$dest_file."';</script>"; ?>
The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1003823.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1003823.htmlTechArticlephp uses tag replacement to generate static pages. PHP can use the built-in function preg_replace to batch replace with an array, but with Regular expression replacement is very inefficient and inconvenient to use...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template