如何将PHP输出为文本

时间:2015-05-27 13:36:27

标签: php html server

我正在制作一个小应用程序让我从浏览器编辑文件。通常这些将是PHP文件。我试图找出一种方法,我可以在页面上使用PHP,同时将文件的内容打印为text / plain,这样它们就不会被执行。

我已经研究过设置

header("content-type: text/plain");

但是,如果我在页面上没有包含这些PHP渲染,而我希望我正在打印的文件是纯文本。由于服务器一直在执行PHP,我也玩过代码和预标签无效。

4 个答案:

答案 0 :(得分:4)

您应该在输出正常编辑页面时不做任何改动。

您可以使用例如file_get_contents()来阅读php文件,并直接在textarea中输出。请注意,您需要确保php文件的内容不会破坏html:

<textarea name="contents">
<?php 
echo htmlspecialchars(file_get_contents('your_file_to_be_edited.php'));
?>
</textarea>

答案 1 :(得分:3)

我想你可能正在寻找highlight_file()。它甚至包括语法高亮。

就这样使用它:

public static void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

答案 2 :(得分:2)

您可以使用htmlspecialchars函数(将特殊字符转换为HTML实体):

highlight_file("phpFile.php"); 

还有一个相反的功能 - htmlspecialchars_decode(将特殊HTML实体转换回字符)。

答案 3 :(得分:0)

下载:

<?php
header('Content-disposition: attachment; filename=gen.txt');
header('Content-type: text/plain');


echo "this is the file\n";
echo " you could generate content here, instead.";
?>

显示:

highlight_file("yourFile.php"); 
相关问题