PHP的file_get_contents与php完好无损? [英] PHP file_get_contents with php intact?

查看:63
本文介绍了PHP的file_get_contents与php完好无损?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与使用include,在文件中执行包含的php相反...是否可以将php文件的内容保存到变量 - 但是php仍然完整且可执行?

As opposed to using an include, which executes the included php in the file...is it possible to save the contents of a php file to a variable - but with the php still intact and executable?

我的目标如下:

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

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

I know thats a dumb example...but I hope it illustrates the general idea. If I have to loop through the template 50 times on a page (say it is a list of blogs), it seems dumb to actually run and include for each.

Am我错了?有办法吗?

Am I wrong? Is there a way to do this?

推荐答案

这个怎么样...

function getTemplate($file) {

    ob_start(); // start output buffer

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

}

基本上,这将获得任何 $ file 是,并用PHP解析它,然后将输出返回到变量。

Basically, this will get whatever $file is, and parse it with PHP, then return the output into a variable.

这篇关于PHP的file_get_contents与php完好无损?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆