静态页面 - 求php静态网页生成方案
ringa_lee
ringa_lee 2017-04-10 17:20:43
[PHP讨论组]

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

ringa_lee
ringa_lee

ringa_lee

全部回复(7)
大家讲道理

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

阿神

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

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

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

巴扎黑

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

ringa_lee

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

天蓬老师

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

高洛峰

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

<?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);
?>

使用也很简便。

ringa_lee
<?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;
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号