How to display HTML tags as plain text

PhpHtml

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

htmlspecialchars('<strong>something</strong>')

Solution 3 - Php

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>';

Solution 4 - Php

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;

Solution 5 - Php

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;
?>

Solution 6 - Php

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;

Solution 7 - Php

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

Solution 8 - Php

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>

Solution 9 - Php

>The native JavaScript approach -

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

>Enjoy!

Solution 10 - Php

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

Solution 11 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser517593View Question on Stackoverflow
Solution 1 - PhpDarmView Answer on Stackoverflow
Solution 2 - PhpacmeView Answer on Stackoverflow
Solution 3 - PhpJarrodView Answer on Stackoverflow
Solution 4 - PhpLuiz DamimView Answer on Stackoverflow
Solution 5 - PhpNikunj K.View Answer on Stackoverflow
Solution 6 - PhpJames MontagneView Answer on Stackoverflow
Solution 7 - PhpPaulo HenriqueView Answer on Stackoverflow
Solution 8 - PhpmartynthewolfView Answer on Stackoverflow
Solution 9 - PhpSimon BorskyView Answer on Stackoverflow
Solution 10 - PhpAlessandroView Answer on Stackoverflow
Solution 11 - PhpReubenView Answer on Stackoverflow