file_get_contents 和 php 代码

发布于 2024-10-04 08:56:10 字数 454 浏览 0 评论 0原文

您好,有没有办法在处理 php 文件中的 php 代码时获取 php 文件的内容? 我需要将代码包含到另一页中,但我需要将其内容打印在特定位置。

如果我使用 include,代码将在 html 标签之前打印,当然是因为它在页面之前进行处理,但是如果我使用 file_get_contents,内容会在页面中获取,但如果我有一些 php 标签,我也会将它们作为纯文本获取。

谢谢。

编辑:对不起,伙计们,我在写作时似乎喝醉了。我纠正了。

我有一个处理页面内容的引擎,将它们放入变量中,然后将它们打印在 html 页面中的特定位置。在引擎中,我需要“嵌入”其他可能有一些 php 标签的“静态”页面的代码。如果我使用 file_get_contents 我将以纯文本形式获取内容(未解析 php 标签),如果我使用 include 它就不起作用,因为它不是它的函数。所以我需要的是将已处理的代码嵌入到引擎中(作为可打印的 HTML)。

Hello is there a way to take the content of a php file while processing the php code in it?
I need to include a code into another page but I need to print the content it in a specific position.

If I use include the code will be printed before the html tag of course cause it's processed before the page, but if I use file_get_contents the content is taken in the page but if I have some php tags I'll get those too as plain text.

Thank you.

EDIT: sorry guys seems like I was drunk while I was writing. I corrected.

I have an engine that processes the page contents, puts them into a variable and then print them in a specific position within the html page. In the engine I need to "embed" the code of other "static" pages that could have some php tags. If I use file_get_contents I will get the content as plain text (with php tags not parsed) if I use include it just won't work cause it's not the function for it. So I what I need is to embed the PROCESSED code into the engine (as ready-to-be-printed HTML).

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

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

发布评论

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

评论(3

灯下孤影 2024-10-11 08:56:10

您可以使用输出缓冲区来包含 PHP 文件,但保存结果输出以供以后使用,而不是立即将其打印到浏览器。

ob_start();
include('path/to/file.php');
$output = ob_get_contents();
ob_end_clean();

// now do something with $output

You can use an output buffer to include a PHP file but save the resulting output for later use instead of printing it immediately to the browser.

ob_start();
include('path/to/file.php');
$output = ob_get_contents();
ob_end_clean();

// now do something with $output
╰つ倒转 2024-10-11 08:56:10

你的问题不是 100% 清楚,但看起来你想包含一个远程 php 源,你可以使用 include("http://domain.com/path/to/php.phps");而不是 file_get_contents。

您说您只需要远程代码的一部分,如果是这样,您可以使用 file_get_contents,使用 substr 或 reg exp 获取这部分代码保存在本地文件上并包含或执行 eval。

这些解决方案都不安全也不推荐,您永远不应该包含远程文件来直接评估您的代码。

Your question is not 100% clear, but it looks like you want to include a remote php source, you can use include("http://domain.com/path/to/php.phps"); instead of a file_get_contents.

You said that you just need a part of the remote code, if it's that, you can use the file_get_contents, get this portion of the code with substr or reg exp save on a local file and include or do a eval.

None of the solutions are secure nor recommended, you should never include remote files to direct evaluation on your code.

∞梦里开花 2024-10-11 08:56:10

如果我理解正确的话;您正在尝试加载一个 php 文件以读取其内容并同时执行它。

IE:
include('/foo/bar')

file_get_contents('/foo/bar')

以下函数执行这两项操作,并返回文件的内容(假设该文件存在)。

function load_and_process($path) {
    if(file_exists($path) {
        include($path);
        return file_get_contents($path);
    }
    return false;
}

并这样称呼它:

if($file_contents = load_and_process('/foo/bar')) {
    echo "Loaded $file_contents from /foo/bar";
}

If I understand correctly; you are trying to load a php file in to read its contents and execute it at the same time.

ie:
include('/foo/bar')
and
file_get_contents('/foo/bar')

The following function does both and returns the contents of the file assuming it exists.

function load_and_process($path) {
    if(file_exists($path) {
        include($path);
        return file_get_contents($path);
    }
    return false;
}

and call it like so:

if($file_contents = load_and_process('/foo/bar')) {
    echo "Loaded $file_contents from /foo/bar";
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文