<!DOCTYPE html> <html> <head> <title>{title}</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div>{content}</div> </body> </html>
tmp.html是模板文件php
<?php /* * 说明:生成静态页面,tmp.html是模板文件,news.html是要生成的文件, * */ //1,先读取模板中内容 $str=file_get_contents('tmp.html'); //2,将指定的内容进行替换 $title='网站标题'; $content='网站内容'; //title和content变量能够是从数据库中读取的内容,在此只是举例 $str=str_replace('{title}',$title,$str); $str=str_replace('{content}',$content,$str); //3,将替换后的内容写入新文件 file_put_contents('news.html',$str);
demo.php是要执行的php脚本程序html
<!DOCTYPE html> <html> <head> <title>网站标题</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div>网页内容</div> </body> </html>
news.html是程序执行完成生成的静态文件数据库