php生成静态页面代码
本款生成静态页面程序实现原理是做好自定的模板标签,然后由str_replace把标签替换成指定的内容,再由fopen生成指定 文件名的静态页面,这样就OK了,代码如下:
<?php header('content-type:text/html;charset=utf-8'); if (!function_exists('file_get_contents')) { //如果系统没有file_get_contents()函数 function file_get_contents($file) { //自己写file_get_contents()函数 $fp = fopen($file, 'r'); $content = fread($fp, filesize($file)); fclose($fp); return $content; } } $tmp_file = 'template.html'; //模板文件 $content = file_get_contents($tmp_file); //获得模板文件内容 $title = 'title'; //模板变量title要替换的值 $text = 'text'; //模板变量text要替换的值 $content = str_replace('<{title}>', $title, $content); //替换模板变量title $content = str_replace('<{text}>', $text, $content); //替换模板变量text //echo $content; //显示替换后的模板文件内容 makehtml('news.html', $content); //写入生成后的静态文件内容到news.html文件 echo '<a href="news.html" target="_blank">查看文件</a>'; function makehtml($file, $content) { //开源代码phprm.com $fp = fopen($file, 'w'); fwrite($fp, $content); fclose($fp); } ?>
template.html,代码如下:
<!doctype html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>makehtml</title> </head> <body> 这是模板变量title------<{title}> <br /> 这是模板变量text------<{text}> </body> </html>
文章地址:http://www.phprm.com/develop/fs5115.html
转载随意^^请带上本文地址!