6

I trying to show the rendered output thrown by a PHP file as text.

The text should contain the HTML tags as well.

Something like what you'd get when you do a "View Source" on a web page.

How can I achieve this?

maxxon15
  • 1,559
  • 4
  • 22
  • 35

5 Answers5

11

Since you have mentioned that you want the output to be like viewing the source, you can simply declare the content type as plain text at the beginning of your script.

This will render the output as text, and the text file is downloadable.

Ex:

<?php
header("Content-Type: text/plain");
echo '<html><head><title>Hello</title></head><body><p>helloooooo</p></body></html>';
echo $_SERVER['REMOTE_ADDR'];
?>

Hope this will make sense, or else if you want to display this to the user, an alternative way would be to pass the output through htmlspecialchars(); function.

Ex:

$content = '<html><head><title>Hello</title></head><body>p>helloooooo</p></body></html>';
echo htmlspecialchars($content);
Nimeshka Srimal
  • 8,012
  • 5
  • 42
  • 57
  • The first one works... sort of. Now I just need a way to display it without downloading. The second one doesn't really work. It outputs the PHP code that generates some of the HTML code dynamically. – maxxon15 Nov 20 '14 at 08:34
  • Hi, I'm wondering why the php code is outputted because that cannot happen. Can you post the code here? – Nimeshka Srimal Nov 20 '14 at 08:47
  • 2
    That is happening because in his codebase he is using statements like `echo`, `print`, etc. And now he probably has put his total source into quotes... – RichardBernards Nov 20 '14 at 08:48
  • 1
    Actually, it depends on the browser whether the file is displayed, or prompted to download. If you try this in Firefox, it will probably ask you to save the file where as chrome would open and display the text. I'm not 100% certain on this though. – Nimeshka Srimal Nov 20 '14 at 08:56
  • That's what @RichardBernards suggested. But the file that was downloaded was perfect though. +1 – maxxon15 Nov 20 '14 at 09:01
  • 3
    It should be `text/plain`, not `plain/text`. – Lexikos May 22 '15 at 00:09
1

try using php's show_source(); function.

give it a link to your text file e.g.

show_source("/link/to/my_file.html");

and be careful with it because it can expose passwords and other sensitive information

giorgio
  • 10,111
  • 2
  • 28
  • 41
hackitect
  • 141
  • 1
  • 8
  • Already tried that. The file contains php codes which renders as HTML. `show_source` just exposes some of the php code instead. :/ – maxxon15 Nov 20 '14 at 08:18
1

To do this, the easiest way is to capture everything which is sent to the output and buffer it. At the end you can decide whether you want to render it just like you always do, or whether you want to use htmlspecialchars() to display the source.

At the start of your code, place the following statement:

$outputType = 'viewsource';
ob_start();

At the end of your code, add the following:

$output = ob_get_contents();
ob_end_clean();
if($outputType == 'viewsource') {
    echo htmlspecialchars($output);
} else {
    echo $output;
}
RichardBernards
  • 3,146
  • 1
  • 22
  • 30
0

There are multiple ways of doing this, the easiest one being to use pre tags, or you could rename your file from .php to .phps, or use highlight_file($file) which also syntax-highlight your code. There's also file_get_contents() and even show_source() as @hackitect mentioned.

Remember though, any html-code has to be outputted within pre tags and escaped.

Chris Magnussen
  • 1,457
  • 9
  • 14
  • `pre` tags don't really work... see comment above, and renaming file from `.php` to `.phps` does output the HTML, but along with the PHP code inside it. I'm trying to get the _final rendered HTML_ that is output by the PHP file... something like what you would get when you do a "view source" on a webpage – maxxon15 Nov 20 '14 at 08:25
0

Another trick which I actually used to do this was make a hyperlink with the view-source url in it.

Works perfectly on Chrome, Firefox and Opera but not on IE.

<a target="_blank" href="view-source:http://stackoverflow.com/questions/27034642/output-rendered-html-as-plain-text">Get Plain Text</a>

maxxon15
  • 1,559
  • 4
  • 22
  • 35