php:file_get_contents正在剥离php代码

sha*_*ebo 2 php

我正在尝试file_get_contents并输出php代码作为字符串而不进行渲染.想法是获取原始的未渲染文件内容,以便可以在textarea中编辑它们...

// file "foo.php" I'm needing the contents of
<h1>foo</h1>
<? include 'path/to/another/file' ?>

// php file that's calling the file_get_contents
<?php
    echo file_get_contents('foo.php');
?>
Run Code Online (Sandbox Code Playgroud)

上面的代码正在删除foo.php中的php include,它输出:

<h1>foo</h1>
Run Code Online (Sandbox Code Playgroud)

有谁知道如何将foo.php内容作为原始的未呈现字符串输出?

<h1>foo</h1>
<? include 'path/to/another/file' ?>
Run Code Online (Sandbox Code Playgroud)

任何帮助是极大的赞赏!

Jua*_*tés 10

据我所知,除非它在同一台服务器上,否则无法获取php内容.确保您尝试访问本地托管的文件,而不是远程的东西,它应该工作.

此外,如果你试图回应代码,它将尝试解析它,所以传递它htmlspecialchars($source),它应该工作.

像这样的东西:

<?php
    echo "<pre>";
    echo htmlspecialchars(file_get_contents('file.php'));
    echo "</pre>";
?>
Run Code Online (Sandbox Code Playgroud)

将回显php文件的格式化源代码,包括注释和其中的任何其他文本,而不进行解析.因为它看起来对你很重要,我也会说它在DOM显示,因为它不再是代码,现在是文本.您可以将它放在容器内,设置样式并随意使用它.