在 PHP 中,有没有一种方法可以在不使用输出缓冲的情况下将 PHP 文件的输出捕获到变量中?
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
PHP 的一个鲜为人知的功能是能够将包含/必需的文件视为具有返回值的函数调用。
例如:
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:
据我在 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.
在阅读了每个人的建议、阅读了一堆文档并尝试了一些东西之后,我想出了这个:
这是我能得到的最接近的结果,但不幸的是它不起作用。 关键是在文件内容之前包含 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:
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 theeval()
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!
乔里·塞布雷希茨是正确的。
如果 PHP 脚本可通过 HTTP 访问,则可以使用等效且稍微简单的方法:
应该注意的是,使用输出缓冲会更容易占用资源。
Joeri Sebrechts is correct.
An equivalent and slightly easier method is available if the PHP script is HTTP accessible:
It should be noted that using output buffering would be easier on resources.
向 php 页面发出curl 请求,本质上是假装是浏览器。
Do a curl request to the php page, essentially pretending to be the browser.
如果文件是本地文件,您可以做的是将脚本作为字符串加载到变量中,然后对该字符串运行 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.
但是请检查有关 eval 的文档,因为您实际上必须让您调用的文件返回其结果,而不仅仅是回显它们:
https://www.php.net/eval
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
黑客警报! 您可以使用
preg_replace_callback
搜索并替换 PHP 块,通过一些黑客技术自己对 PHP 进行评估。您必须修改要包含的 PHP 文件才能使用 returnOrEcho 函数,以便可以在本例和正常情况下重载它。 在这种情况下,您想要
return
,以便eval
按照您想要的方式拾取它,但正常情况是echo
> 没有回报。因此,对于这种情况,您将定义:
对于正常情况,您将定义:
在包含的 PHP 文件(或视图文件)中,您将有类似这样的内容:
我无法让
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.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 toreturn
so that it will be picked up by theeval
in the way you want, but the normal case is toecho
without a return.So for this case you would define:
and for the normal case you would define:
In your included PHP file (or view file) you would have something like this:
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.