Heim  >  Artikel  >  Backend-Entwicklung  >  静态页面 - 求php静态网页生成方案

静态页面 - 求php静态网页生成方案

WBOY
WBOYOriginal
2016-10-22 00:14:09866Durchsuche

我想在网站中加个导出静态网页的功能,有什么好的方案可以快速导出呢?

回复内容:

我想在网站中加个导出静态网页的功能,有什么好的方案可以快速导出呢?

不是有个file_put_content()的函数吗?

需要用到ob_start() 系列方法,

用curl file_get_contentS 等模拟请求,都将非常低效,各种框架都用ob实现

手机端打码字难,不详说了

可以看一下php中的Output Control函数

何不用wget直接抓个mirror出来然后打个tgz包下载

`ob_start();
//模板处理
//echo 模板内容
$content = ob_get_contents();
ob_end_clean();
file_put_contents('./demo.html', $content);`

也可以用smart模板实现,如下所示:

<code><?php require('smarty/Smarty.class.php');
$t = new Smarty;
$t->assign("title","Hello World!");
$content = $t->fetch("templates/index.htm");
//这里的 fetch() 就是获取输出内容的函数,现在$content变量里面,就是要显示的内容了
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?></code>

使用也很简便。

<code><?php /* 在这里数据库增删改查之前对缓存进行过期判断和应用 */
$app['data'] = db_crud();
$view = render('index.php');
function render($template) {
    global $app;
    ob_end_clean();
    ob_start();
    require APP_ROOT.'/view/'.$template; //模板里会用到数据$app['data']
    $html = ob_get_contents();
    ob_end_clean();
    ob_start();
    /* 在这里把 ob_get_contents 拿到的字符串 file_put_contents 写入文件系统 */
    return $html;
}</code></code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn