在 PHP 中,有没有一种方法可以在不使用输出缓冲的情况下将 PHP 文件的输出捕获到变量中?

发布于 2024-07-08 16:43:42 字数 263 浏览 12 评论 0原文

在 PHP 中,我想将文件读入变量并同时处理文件中的 PHP,而不使用输出缓冲。 这可能吗?

本质上,我希望能够在不使用 ob_start() 的情况下完成此任务:

<?php
ob_start();
include 'myfile.php';
$xhtml = ob_get_clean();
?>

这在 PHP 中可能吗?

更新:我想在输出回调中做一些更复杂的事情(不允许输出缓冲)。

In PHP, I want to read a file into a variable and process the PHP in the file at the same time without using output buffering. Is this possible?

Essentially I want to be able to accomplish this without using ob_start():

<?php
ob_start();
include 'myfile.php';
$xhtml = ob_get_clean();
?>

Is this possible in PHP?

Update: I want to do some more complex things within an output callback (where output buffering is not allowed).

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

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

发布评论

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

评论(8

好菇凉咱不稀罕他 2024-07-15 16:43:42

PHP 的一个鲜为人知的功能是能够将包含/必需的文件视为具有返回值的函数调用。

例如:

// myinclude.php
$value = 'foo';
$otherValue = 'bar';
return $value . $otherValue;


// index.php
$output = include './myinclude.php';
echo $output;
// Will echo foobar

A little known feature of PHP is being able to treat an included/required file like a function call, with a return value.

For example:

// myinclude.php
$value = 'foo';
$otherValue = 'bar';
return $value . $otherValue;


// index.php
$output = include './myinclude.php';
echo $output;
// Will echo foobar
阳光下的泡沫是彩色的 2024-07-15 16:43:42

据我在 PHP 文档中所知,没有。 为什么要避免输出缓冲?

解决这个问题的唯一方法是使用一些 hacky 方法,包括调用命令行 php 客户端或根据可用内容和特定要求执行卷曲请求。

From what I can tell in the PHP documentation, no. Why do you want to avoid output buffering?

The only way to get around this would be hacky methods involving either invoking the command line php client or doing a curl request based on what's available and what the particular requirements are.

荒人说梦 2024-07-15 16:43:42

在阅读了每个人的建议、阅读了一堆文档并尝试了一些东西之后,我想出了这个:

<?php
$file = file_get_contents('/path/to/file.php');
$xhtml = eval("?>$file");
?>

这是我能得到的最接近的结果,但不幸的是它不起作用。 关键是在文件内容之前包含 PHP 结束位 (?>)。 这将使 eval() 脱离 PHP 评估模式,并将文件的内容视为非 PHP 代码。 然后,如果文件中有 PHP 代码块,这些代码块将被评估为 PHP。 令人遗憾的是,它不会将评估的内容保存在变量中,而只是将其输出到页面。

谢谢大家的帮助!

After reading everybody's suggestions, reading a bunch of documentation, and playing around with some things, I came up with this:

<?php
$file = file_get_contents('/path/to/file.php');
$xhtml = eval("?>$file");
?>

It's as close as I could get but it unfortunately doesn't work. The key to this is to include the closing PHP bit (?>) before the contents of the file. This will take the eval() out of PHP-evaluation mode and will treat the contents of the file starting as non-PHP code. Then if there are PHP code blocks within the file, those will be evaluated as PHP. The bummer is that it doesn't save the eval'd content in the variable, it just outputs it to the page.

Thanks for the help everybody!

信仰 2024-07-15 16:43:42

乔里·塞布雷希茨是正确的。
如果 PHP 脚本可通过 HTTP 访问,则可以使用等效且稍微简单的方法:

$data = file_get_contents('http://google.com/');

应该注意的是,使用输出缓冲会更容易占用资源。

Joeri Sebrechts is correct.
An equivalent and slightly easier method is available if the PHP script is HTTP accessible:

$data = file_get_contents('http://google.com/');

It should be noted that using output buffering would be easier on resources.

睡美人的小仙女 2024-07-15 16:43:42

向 php 页面发出curl 请求,本质上是假装是浏览器。

Do a curl request to the php page, essentially pretending to be the browser.

白况 2024-07-15 16:43:42

如果文件是本地文件,您可以做的是将脚本作为字符串加载到变量中,然后对该字符串运行 eval。 然后你就可以做所有其他的事情了。 否则,您必须使用输出缓冲。

What you could do, if the file is local, is load the script into a variable as a string, then run eval on the string. Then you can do all your other stuff afterwards. Otherwise, you have to use output buffering.

归属感 2024-07-15 16:43:42
$fileData = file_get_contents('fileOnDisk.php');
$results = eval($fileData);

但是请检查有关 eval 的文档,因为您实际上必须让您调用的文件返回其结果,而不仅仅是回显它们:

https://www.php.net/eval

$fileData = file_get_contents('fileOnDisk.php');
$results = eval($fileData);

But check the documentation on eval, because you actually have to have the file you are calling return its results rather than just echo them:

https://www.php.net/eval

空宴 2024-07-15 16:43:42

黑客警报! 您可以使用 preg_replace_callback 搜索并替换 PHP 块,通过一些黑客技术自己对 PHP 进行评估。

function evalCallback($matches)
{
    // [0] = <?php return returnOrEcho("hi1");?>
    // [1] = <?php
    // [2] = return returnOrEcho("hi1");
    // [3] = ?>
    return eval($matches[2]);
}

function evalPhp($file)
{
    // Load contents
    $contents = file_get_contents($file);
    // Add returns
    $content_with_returns = str_replace(
                               "returnOrEcho"
                              ,"return returnOrEcho"
                              ,$contents);
    // eval
    $modified_content = preg_replace_callback(
                              array("|(\<\?php)(.*)(\?\>)|"
                             ,"evalCallback"
                             ,$content_with_returns);
    return $modified_content;
}

您必须修改要包含的 PHP 文件才能使用 returnOrEcho 函数,以便可以在本例和正常情况下重载它。 在这种情况下,您想要return,以便eval按照您想要的方式拾取它,但正常情况是echo > 没有回报。

因此,对于这种情况,您将定义:

function returnOrEcho($str)
{
    return $str;
}

对于正常情况,您将定义:

function returnOrEcho($str)
{
    echo $str;
}

在包含的 PHP 文件(或视图文件)中,您将有类似这样的内容:

<?php returnOrEcho("hi1");?>
<?php returnOrEcho("hi3"."oo");?>
<?php returnOrEcho(6*7);?>

我无法让 preg_replace_callback 内联回调工作所以我使用了一个单独的函数,但有一个如何执行此操作的示例: preg_replace_callback() - 当前对象实例内的回调

Hack Alert! You could do the evaluation of the PHP yourself with a bit of hackery using preg_replace_callback to search and replace the PHP blocks.

function evalCallback($matches)
{
    // [0] = <?php return returnOrEcho("hi1");?>
    // [1] = <?php
    // [2] = return returnOrEcho("hi1");
    // [3] = ?>
    return eval($matches[2]);
}

function evalPhp($file)
{
    // Load contents
    $contents = file_get_contents($file);
    // Add returns
    $content_with_returns = str_replace(
                               "returnOrEcho"
                              ,"return returnOrEcho"
                              ,$contents);
    // eval
    $modified_content = preg_replace_callback(
                              array("|(\<\?php)(.*)(\?\>)|"
                             ,"evalCallback"
                             ,$content_with_returns);
    return $modified_content;
}

You would have to modify the PHP file you are including to use a returnOrEcho function so that it can be overloaded for this case and the normal case. In this case you want to return so that it will be picked up by the eval in the way you want, but the normal case is to echo without a return.

So for this case you would define:

function returnOrEcho($str)
{
    return $str;
}

and for the normal case you would define:

function returnOrEcho($str)
{
    echo $str;
}

In your included PHP file (or view file) you would have something like this:

<?php returnOrEcho("hi1");?>
<?php returnOrEcho("hi3"."oo");?>
<?php returnOrEcho(6*7);?>

I couldn't get preg_replace_callback inline callback working so I used a separate function, but there is an example of how to do it: preg_replace_callback() - Calback inside current object instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文