Home  >  Article  >  Backend Development  >  put your head on my shoulder php 静态化实现代码

put your head on my shoulder php 静态化实现代码

WBOY
WBOYOriginal
2016-07-29 08:39:39852browse

模板文件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

复制代码 代码如下:




文章标题


文章标题




这里是文章主体



以上就介绍了put your head on my shoulder php 静态化实现代码,包括了put your head on my shoulder方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

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 [email protected]