php生成静态页

<?php
//获取要生成静态页面的内容,这里url可以是php
$content = file_get_contents('http://www.w3school.com.cn/php/');
//将内容写入到以时间命名的html文件中
if (file_put_contents(date('YmdHis').'.html',$content)){
    echo 'success';
} else {
    echo 'fail';
}
?> 
静态化的好处,自己找吧。

今天 介绍二种方法。

第一种方法是真静态的。

replace.php
[php] view plaincopy

    <?php  
    function replace($row){  
        //变量替换  
        $title="文章标题";  
        $body="这里是主体";  
        //参数中的关键字  
        $row=str_replace("%title%",$title,$row);  
        $row=str_replace("%body%",$body,$row);  
        //返回替换的结果  
        return $row;  
    }  
    //模板文件指针  
    $f_tem=fopen("temp.htm","r");  
    //生成文件指针  
    $f_new=fopen("new.html","w");  
    //循环读模板文件,每次一行  
    while(!feof($f_tem)){  
        $row=fgets($f_tem);  
        $row=replace($row);//替换读入内容的关键字  
        fwrite($f_new,$row);//将替换后的内容写入生成 的html文件  
    }  
    //关才指针  
    fclose($f_new);  
    fclose($f_tem);  
    ?>  


new.html

[html] view plaincopy

    <!DOCTYPE HTML>  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title>文章标题</title>  
      
    </head>  
    <body>  
    <h1>文章标题</h1>  
    <hr>  
    这里是主体  
      
    </body>  
    </html>  


temp.htm

[html] view plaincopy

    <!DOCTYPE HTML>  
    <html>  
    <head>  
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
    <title>%title%</title>  
      
    </head>  
    <body>  
    <h1>%title%</h1>  
    <hr>  
    <pre>%body%</pre>  
      
    </body>  
    </html>  
原文地址:https://www.cnblogs.com/afei-happy/p/3491880.html