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?
011 Answers
Replace < with < and > with >.
In PHP use the function htmlspecialchars() to escape < and >.
htmlspecialchars('<strong>something</strong>') 0As 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>'; 0You should use htmlspecialchars. It replaces characters as below:
&(ampersand) becomes&"(double quote) becomes"when ENT_NOQUOTES is not set.'(single quote) becomes'only when ENT_QUOTES is set.<(less than) becomes<>(greater than) becomes>
you may use htmlspecialchars()
<?php $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES); echo $new; // <a href='test'>Test</a> ?> You just need to encode the <>s:
<strong>Look just like this line - so then know how to type it</strong> To display HTML tags within a browser, surround the output with < xmp> and < / xmp> tags
2You can use htmlentities when echoing to the browser, this will show the tag rather than have html interpret it.
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> The native JavaScript approach -
('<strong>Look just ...</strong>').replace(/</g, '<').replace(/>/g, '>'); 2Enjoy!
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...
Use htmlentities() to convert characters that would otherwise be displayed as HTML.
0