【问题标题】:PHP file_get_contents, execute the PHP on the included file before includePHP file_get_contents,在 include 之前对包含的文件执行 PHP
【发布时间】:2012-06-10 12:51:39
【问题描述】:

目前我使用 file_get_contents 命令来包含文件的 html。现在我向它添加了 PHP,它导致了一个问题。 PHP 被读取为 html,因此未执行。如何在 PHP 执行后检索文件内容?

哦,我需要字符串中的内容。所以我相信在这种情况下 include 不起作用,对吧?

谢谢!

【问题讨论】:

    标签: php html file-get-contents


    【解决方案1】:
    ob_start();
    include 'file_to_include.php';
    $contents = ob_get_clean();
    

    包含文件的内容(正在执行 PHP)在$contents

    【讨论】:

    • 你可以使用ob_get_flush()并去掉最后一个函数。
    • @JaredFarrish 我将它添加到我的答案中。谢谢。
    • 我试过了。它可能会按照要求将包含放在字符串中,但它也会在设置包含的区域中包含文件。不能允许这种情况发生。有什么想法吗?
    • 我编辑了你的答案,应该很快就会看到变化。 ob_get_clean 是我要找的 ;)
    猜你喜欢
    • 1970-01-01
    • 2016-03-08
    • 1970-01-01
    • 2012-02-04
    • 2012-06-09
    • 2013-05-20
    • 1970-01-01
    • 2013-09-19
    • 1970-01-01
    相关资源
    最近更新 更多