How to display HTML tags as plain text [duplicate]
Asked Answered
P

11

260

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?

Pugilism answered 25/7, 2011 at 13:58 Comment(0)
A
372

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

Atheroma answered 25/7, 2011 at 14:0 Comment(4)
Technically you only need to replace < by &lt; for it to be recognized by most browsers, but the > by &gt; is good practiceStieglitz
@Atheroma -1 for not mentioning htmlspecialchars(). If there's a justified reason for that please correct me :)Severally
If the question's text were to be generated by PHP, htmlspecialchars() does what this answer suggests: replace the less-than and greater-than signs (and others) with their HTML entities. But the question does not specify, so this more general answer is a superset of all the PHP-specific answers. And the question is really about HTML.Sandarac
In contrary of the other comments this answer saves me 100%, some of the answers I found in here would say just use php and echo "<? //like this ?>"; but that didn't help at all still not showing. On the other hand this answer saves me 17 characters in total plus @Sandarac has a point so voted up.Arbour
O
258

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

htmlspecialchars('<strong>something</strong>')
Olibanum answered 25/7, 2011 at 14:2 Comment(0)
T
76

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>';
Tardif answered 25/3, 2013 at 22:5 Comment(0)
V
44

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;
Vidovik answered 25/7, 2011 at 14:0 Comment(0)
S
13

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;
?>
Sinistrocular answered 29/10, 2013 at 13:19 Comment(0)
S
8

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;
Stagger answered 25/7, 2011 at 14:0 Comment(0)
D
6

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

Dyeing answered 22/6, 2014 at 14:38 Comment(2)
<xmp> is obsolete now, more than just deprecated. Don't use it.Lagting
A quick Google search on W3C reveals that XMP was introduced for displaying preformatted text in HTML 3.2 and earlier. When W3C deprecated the XMP tag, it suggested using the PRE tag as a preferred alternative. Update: w3.org/TR/REC-html32#xmp, w3.org/MarkUp/html-spec/html-spec_5.html#SEC5.5.2.1 share edit follow flagMaye
S
4

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

See here https://www.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>
Scalping answered 25/7, 2011 at 14:3 Comment(0)
F
3

The native JavaScript approach -

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

Enjoy!

Folkmoot answered 18/4, 2019 at 12:30 Comment(2)
JavaScript isn't tagged in this post.Vegetative
Thank you. It's usefull for me.Wholism
P
1

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

Paulie answered 25/7, 2011 at 14:3 Comment(0)
B
1

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...

Bucky answered 1/9, 2018 at 12:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.