how do you write html code so the user and see it on a webpage (like a how-to for html)
+6
A:
encode your html entities:
< … <
> … >
& … &
" … "
(' … ' xml, not html. see comments)
you might also want to use
<pre><code>
here comes your preformatted and escaped <html>-code
</code></pre>
to have your code monospaced and preserve whitespaces
knittl
2009-09-08 15:22:50
doesn't <verbatim> give you the same? i thought <pre> will still expand any html tags found within?
Rob Wells
2009-09-08 15:35:30
' isn't supported by IE because it isn't a valid character entity in HTML. The list of HTML character entities is at http://www.w3.org/TR/html401/sgml/entities.html and doesn't include it. It is specified in XML 1.0: http://www.w3.org/TR/2008/REC-xml-20081126/#syntax
NickFitz
2009-09-08 15:36:59
Hmm, SO manages to escape ' in comments but not in posts...
NickFitz
2009-09-08 15:37:50
@Rob: What do you mean by "expand"? In general, there is *no* section of html that is not escaped - not even script tags or style tags (browsers will generally try to to repair missing escaping as best they can though - and a long time ago).
Eamon Nerbonne
2009-09-08 15:41:36
@ rob wells: there is no such tag as `<verbatim>`@ nickfitz: see <http://www.w3.org/TR/2008/REC-xml-20081126/#sec-predefined-ent>. looks like ' :P
knittl
2009-09-08 15:41:51
@knittl: if you're programmatically encoding illegal codepoints, it is possibly simpler to use numeric entity references, whether decimal or hexadecimal - and you won't run into details like apos being html 5 or xml but not html 4.
Eamon Nerbonne
2009-09-08 15:46:30
@ eamon: using builtin functions like php’s htmlspecialchars solves the problem perfectly :) (but yeah, i got html and xml confused)
knittl
2009-09-08 15:51:27
@knittl: HTML is not XML, and ' is not an HTML entity. _XHTML_ allows ' because it _is_ XML, but IE doesn't support XHTML, so it doesn't support ' See the XHTML 1.0 spec: http://www.w3.org/TR/xhtml1/#C_16
NickFitz
2009-09-08 16:20:42
nickfitz, i already got it ;)
knittl
2009-09-08 16:33:03
@knittl: Oops, so I see - sorry to nag ;-)
NickFitz
2009-09-08 16:40:51
A:
You have to use HTML character entities <
and >
in place of the < and > symbols so they aren't interpreted as HTML tags.
Jonathan Patt
2009-09-08 15:25:46