Home  >  Article  >  Backend Development  >  php 静态化实现代码_PHP

php 静态化实现代码_PHP

WBOY
WBOYOriginal
2016-06-01 12:23:43780browse

模板文件template.htm:
复制代码 代码如下:


%title%


%title%




%body%





php文件:
复制代码 代码如下:
//Replace函数用于将从模版文件中读取的内容中的关键字替换成变量中的内容
function Replace($row)
{
//定义用来替换的变量
$title = "文章标题";
$body = "这里是文章主体";
//替换参数中的关键字
$row = str_replace("%title%", $title, $row);
$row = str_replace("%body%", $body, $row);
//返回替换后的结果
return $row;
}
//模版文件指针
$f_tem = fopen("template.htm","r");
//生成的文件指针
$f_new = fopen("new.htm","w");
//循环读取模版文件,每次读取一行
while(!feof($f_tem))
{
$row = fgets($f_tem);
$row = Replace($row); //替换读入内容中的关键字
fwrite($f_new, $row); //将替换后的内容写入生成的HTML文件
}
//关闭文件指针
fclose($f_new);
fclose($f_tem);
?>

生成新的html页:new.html
复制代码 代码如下:


文章标题


文章标题




这里是文章主体




Statement:
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