将文件包含到变量中

发布于 2024-10-21 19:18:35 字数 636 浏览 3 评论 0原文

我试图保持我的代码干净,将其中一些分解成文件(有点像库)。但其中一些文件需要运行 PHP。

所以我想做的是这样的:

$include = include("file/path/include.php");
$array[] = array(key => $include);

include("template.php");

比在 template.php 中我会:

foreach($array as $a){
    echo $a['key'];
}

所以我想将 php 运行后发生的事情存储在变量中以便稍后传递。

使用 file_get_contents 不会运行 php,它会将其存储为字符串,所以有什么选项可以做到这一点还是我运气不好?

更新:

就像:

function CreateOutput($filename) {
  if(is_file($filename)){
      file_get_contents($filename);
  }
  return $output;
}

或者您的意思是为每个文件创建一个函数?

I am trying to keep my code clean break up some of it into files (kind of like libraries). But some of those files are going to need to run PHP.

So what I want to do is something like:

$include = include("file/path/include.php");
$array[] = array(key => $include);

include("template.php");

Than in template.php I would have:

foreach($array as $a){
    echo $a['key'];
}

So I want to store what happens after the php runs in a variable to pass on later.

Using file_get_contents doesn't run the php it stores it as a string so are there any options for this or am I out of luck?

UPDATE:

So like:

function CreateOutput($filename) {
  if(is_file($filename)){
      file_get_contents($filename);
  }
  return $output;
}

Or did you mean create a function for each file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋檐 2024-10-28 19:18:35

看来您需要使用 输出缓冲控制 -- 特别参见 ob_start()ob_get_clean() 函数。

使用输出缓冲将允许您将标准输出重定向到内存,而不是将其发送到浏览器。

Here's a quick example :

// Activate output buffering => all that's echoed after goes to memory
ob_start();

// do some echoing -- that will go to the buffer
echo "hello %MARKER% !!!";

// get what was echoed to memory, and disables output buffering
$str = ob_get_clean();

// $str now contains what whas previously echoed
// you can work on $str

$new_str = str_replace('%MARKER%', 'World', $str);

// echo to the standard output (browser)
echo $new_str;

您将得到的输出是:

hello World !!!

It seems you need to use Output Buffering Control -- see especially the ob_start() and ob_get_clean() functions.

Using output buffering will allow you to redirect standard output to memory, instead of sending it to the browser.

Here's a quick example :

// Activate output buffering => all that's echoed after goes to memory
ob_start();

// do some echoing -- that will go to the buffer
echo "hello %MARKER% !!!";

// get what was echoed to memory, and disables output buffering
$str = ob_get_clean();

// $str now contains what whas previously echoed
// you can work on $str

$new_str = str_replace('%MARKER%', 'World', $str);

// echo to the standard output (browser)
echo $new_str;

And the output you'll get is :

hello World !!!
陌路黄昏 2024-10-28 19:18:35

您的 file/path/include.php 是什么样子的?

您必须通过http调用file_get_contents来获取它的输出,例如,

$str = file_get_contents('http://server.tld/file/path/include.php');

最好修改您的文件以通过函数输出一些文本:

<?php

function CreateOutput() {
  // ...
  return $output;
}

?>

而不是在包含它之后,调用该函数来获取输出。

include("file/path/include.php");
$array[] = array(key => CreateOutput());

How does your file/path/include.php look like?

You would have to call file_get_contents over http to get the output of it, e.g.

$str = file_get_contents('http://server.tld/file/path/include.php');

It would be better to modify your file to output some text via a function:

<?php

function CreateOutput() {
  // ...
  return $output;
}

?>

Than after including it, call the function to get the output.

include("file/path/include.php");
$array[] = array(key => CreateOutput());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文