any alternative of <xmp> tag to renders html to as plain text [duplicate]
Asked Answered
B

2

5

I want to show HTML code as plain text.
for example

<div style="width:250px;">
<xmp>
<a href="#"> new element  dummy text new element  dummy text new element  dummy text new element  dummy text new element  dummy text</a>
</xmp>
<div>

The problem is while using <xmp> tag its ignore the div width that contain text. How can I fix it out. Any other solution instead of <xmp> tag?

Blueing answered 9/1, 2016 at 10:43 Comment(5)
Are you using only HTML or is it in combination with some serverside language. I.E. PHP?Whitelaw
And also, <xmp> tag is deprecated since a long time. Use its replacement <pre> tag.Whitelaw
i try <pre> tag but it does not render html as plain text. i want to show source code as plain text.Blueing
yes i am using php to get source code from db and display it on screen as plain textBlueing
it is good form to accept the answer if it workedGalliot
W
2

Simply using <xmp> or <pre> tag won't help you display HTML as plain text. The just help to format the text. In order to show HTML to your users you need to escape all special characters.

For example, replace all < signs with &lt; and all > signs with &gt;.

I've shown just two symbols, a quick google search will help you to find all the symbols and their replacements.

And in case you're using PHP, it has built in function to do this. i.e. htmlentities($str). Search for whatever language you are using.

Note that <xmp> Tag has been deprecated.

Whitelaw answered 9/1, 2016 at 10:57 Comment(6)
i am getting dynamic data that contain html tags .that's why i am not able to replace every tag symbol with its symbol code. is any way to display all data as plain text like <xmp> tag do.Blueing
I just said it. do echo htmlentities($data); inside your <pre> tag.Whitelaw
thanks a lot. its work.Blueing
Never mind. Just accept the answer then :DWhitelaw
@NijrajGelani You say "Simply using <xmp> (..) tag won't help you display HTML as plain text." but actually it does!Schram
@MrLister I wasn't aware of that, thank you. I've corrected the mistake. Feel free to edit further... :DWhitelaw
A
1
<div style="width:250px; background:yellow;">
<pre>
<script type="text/plain" style="display:block;">
<a href="#"> new element  dummy 
text new element  dummy text new 
element 
element 
dummy text new element 
dummy text new element 
dummy text</a>
</script>
</pre>
<div>
Adverbial answered 23/8, 2019 at 12:0 Comment(1)
try wrapping HTML content in <textarea></textarea> For ex: <pre> <textarea> <html> </html> </textarea> </pre> Works in awesome way. You don't have to use obsolete <XMP> example tag. <textarea> tag will wrap the content in text area. Try out !Priester

© 2022 - 2024 — McMap. All rights reserved.