加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP的file_get_contents与php完好无损?

发布时间:2020-12-13 22:37:39 所属栏目:PHP教程 来源:网络整理
导读:与使用include,在文件中执行包含的php相反…是否可以将php文件的内容保存到变量 – 但是php仍然完整且可执行? 我的目标看起来像: $template = some_imaginary_include_function('myfile.php');foreach($list_of_blogs as $blog) { // somehow get blog con
与使用include,在文件中执行包含的php相反…是否可以将php文件的内容保存到变量 – 但是php仍然完整且可执行?

我的目标看起来像:

$template = some_imaginary_include_function('myfile.php');
foreach($list_of_blogs as $blog) {
    // somehow get blog content in template and render template;
}

我知道那是一个愚蠢的例子……但我希望它能说明一般的想法.如果我必须在页面上循环模板50次(比如它是一个博客列表),实际运行并包含每个模板似乎都很愚蠢.

我错了吗?有没有办法做到这一点?

这个怎么样…
function getTemplate($file) {

    ob_start(); // start output buffer

    include $file;
    $template = ob_get_contents(); // get contents of buffer
    ob_end_clean();
    return $template;

}

基本上,这将得到任何$文件,并用PHP解析它,然后将输出返回到变量.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读