生成静态页面流程图
二、功能实现
设置example.html为模板文件,然后按照此模板文件生成article-1.html~article-5.html,以此来做简单的演示,代码如下:
<?php//将数据存入二维数组$con=array(array("文章标题1","文章内容1"),array("文章标题2","文章内容2"),array("文章标题3","文章内容3"),array("文章标题4","文章内容4"),array("文章标题5","文章内容5"));foreach($con as $id=>$val){ //循环生成 $title=$val[0]; $content=$val[1]; $path="article-".($id+1).".html"; //替换example内容,并获取内容赋值给$str $fp=fopen("example.html","r"); $str=fread($fp,filesize("example.html")); $str=str_replace("{title}",$title,$str); $str=str_replace("{content}",$content,$str); fclose($fp); //新建空白文件,将$str写入 $handle=fopen($path,"w"); fwrite($handle,$str); fclose($handle); echo "生成".$path."<br/>";}?>}注解:
fopen
(文件名,打开方式),打开文件函数,若无文件,则创建。其返回值为资源型;fread
(文件名,读取字节数),读取文件内容及对应的字节数;str_replace
(规定要查找的值,替换被查找值的值,被搜索的字符串),替换函数;fclose
(文件名),关闭文件;fwrite
(要写入的打开文件,要写入打开文件的字符串,要写入的最大字节数)。