Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display HTML tags as plain text [duplicate]

Tags:

html

php

I have an input form on my website where HTML is allowed and I'm trying to add instructions about the use of HTML tags. I'd like the text to

<strong>Look just like this line - so then know how to type it</strong>

But so far all I get is:

Look just like this line - so then know how to type it

How can I show the tags so people know what to type?

like image 440
user517593 Avatar asked Jul 25 '11 13:07

user517593


People also ask

Why is my HTML code showing up as text?

Show activity on this post. This HTML isn't being interpreted as plain text: it's being interpreted as HTML. It's just that the contents of your HTML include markup that has been escaped, such as &lt; and &gt; .

How do I display HTML code without executing?

Use HTML Special Character Codes var myCode = "<b>This is not bold</b>"; $('span#code-span'). text(myCode); Using text instead of html will cause tags to be rendered exposed instead of being executed.

How do I display HTML code in HTML page?

Right-click a blank part of the web page and select Show Page Source from the pop-up menu that appears. Once the developer options are enabled, you can also press Command + option + U to view the source code.


11 Answers

Replace < with &lt; and > with &gt;.

like image 122
Darm Avatar answered Sep 30 '22 12:09

Darm


In PHP use the function htmlspecialchars() to escape < and >.

htmlspecialchars('<strong>something</strong>')
like image 24
acme Avatar answered Sep 30 '22 12:09

acme


As many others have said, htmlentities() will do the trick... but it will look like shit.

Wrap it up with a <pre> tag and you'll preserve your indentation.

echo '<pre>';
echo htmlspecialchars($YOUR_HTML);
echo '</pre>';
like image 40
Jarrod Avatar answered Sep 30 '22 11:09

Jarrod


You should use htmlspecialchars. It replaces characters as below:

  • & (ampersand) becomes &amp;
  • " (double quote) becomes &quot; when ENT_NOQUOTES is not set.
  • ' (single quote) becomes &#039; only when ENT_QUOTES is set.
  • < (less than) becomes &lt;
  • > (greater than) becomes &gt;
like image 24
Luiz Damim Avatar answered Sep 30 '22 11:09

Luiz Damim


you may use htmlspecialchars()

<?php
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
?>
like image 21
Nikunj K. Avatar answered Sep 30 '22 12:09

Nikunj K.


You just need to encode the <>s:

&lt;strong&gt;Look just like this line - so then know how to type it&lt;/strong&gt;
like image 45
James Montagne Avatar answered Sep 30 '22 11:09

James Montagne


To display HTML tags within a browser, surround the output with < xmp> and < / xmp> tags

like image 38
Paulo Henrique Avatar answered Sep 30 '22 12:09

Paulo Henrique


You can use htmlentities when echoing to the browser, this will show the tag rather than have html interpret it.

See here http://uk3.php.net/manual/en/function.htmlentities.php

Example:

 echo htmlentities("<strong>Look just like this line - so then know how to type it</strong>"); 

Output:

<strong>Look just like this line - so then know how to type it</strong>
like image 20
martynthewolf Avatar answered Sep 30 '22 13:09

martynthewolf


The native JavaScript approach -

('<strong>Look just ...</strong>').replace(/</g, '&lt;').replace(/>/g, '&gt;');

Enjoy!

like image 22
Simon Borsky Avatar answered Sep 30 '22 11:09

Simon Borsky


There is another way...

header('Content-Type: text/plain; charset=utf-8');

This makes the whole page served as plain text... better is htmlspecialchars...

Hope this helps...

like image 25
Alessandro Avatar answered Sep 30 '22 13:09

Alessandro


Use htmlentities() to convert characters that would otherwise be displayed as HTML.

like image 44
Reuben Avatar answered Sep 30 '22 13:09

Reuben