gpt4 book ai didi

PHP file_get_contents 与 php 完好无损?

转载 作者:可可西里 更新时间:2023-11-01 12:25:35 27 4
gpt4 key购买 nike

与使用 include 相反,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;

}

基本上,这将获取 $file 是什么,并用 PHP 解析它,然后将输出返回到一个变量中。

关于PHP file_get_contents 与 php 完好无损?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2061032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com