php生成静态页面代码
本款生成静态页面程序实现原理是做好自定的模板标签,然后由str_replace把标签替换成指定的内容,再由fopen生成指定 文件名的静态页面,这样就OK了,代码如下:
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 ){ //开源代码phpfensi测试数据 $fp = fopen ( $file , 'w' ); fwrite( $fp , $content ); fclose( $fp ); }template.html,代码如下:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://HdhCmsTestw3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd" > < html xmlns = "http://HdhCmsTestw3.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 >查看更多关于php生成静态页面代码 - php高级应用的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did30199