Advertisement
Advertisement


How to display HTML tags as plain text


Question

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?

2015/12/23
1
215
12/23/2015 5:17:40 PM

Accepted Answer

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

2020/01/23
304
1/23/2020 3:42:43 PM


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>';
2019/10/23

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;
2011/07/25

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;
?>
2013/10/29

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;
2011/07/25

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

2014/06/22

Source: https://stackoverflow.com/questions/6817262
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]