views:

32

answers:

5

i am getting html from the database, and want to output the html as text without the browser rendering the HTML.

I have unsuccessfully tried to set the content type to text/plain.

I have tried using the textarea but the code is not formatted.

+1  A: 

You need to HTML escape the string before writing it out. How you do this depends on the language you are using.

You can also wrap it in <pre> and <code> tags to preserve the formatting.

E.g.,

<pre><code>

&lt;div&gt;my HTML markup&lt;/div&gt;

</code></pre>
RedFilter
A: 

<?php echo htmlentities($dbcontent); ?>

http://php.net/htmlentities

john010117
A: 

I have unsuccessfully tried to set the content type to text/plain.

That should work, though.

Alternatively, consider outputting the HTML with the HTML control characters escaped. In PHP, htmlentities($html) would do the job.

Pekka
A: 

I have unsuccessfully tried to set the content type to text/plain.

It should work. Something else is overriding this behaviour. What browser and URL are you using? MSIE is relying more on file extension in URL than on content type. Rather use name.txt than name.html. What response headers do you get? The webserver might have overriden it. You can use Firebug to get them all. Consult the server documentation how to set the proper headers for certain URL's.

BalusC
A: 

Definitely go htmlentities route, maybe wrap in

<pre>
... If that doesn't work, write something to go through the html and replace "<" with "&lt ;" (minus the space)

Bradley Herman