Skip to content Skip to sidebar Skip to footer

How To Encode Quotes In Html Body?

Should I encode quotes (such as ' and ' -> ” and ’) in my HTML body (e.g. convert

Matt's Stuff

to

Matt’s Stuff

Solution 1:

Encoding quotation marks (") is in practice only needed if the're inside an attribute, however for the HTML code to be correct (passing HTML validation), you should always encode quotation marks as ".

Apostrophes (') don't need escaping in HTML. In XHTML they should be encoded as '.

Solution 2:

If you want your markup to be parsable as XML, you'll want to encode the following:

& => &
< => &lt;
> => &gt;
" => &quot;
' => &apos;

Definitely do this in attributes whether you're trying to make your code XML compliant or not.

Solution 3:

Typicaly such isn't necessary unless you're placing such values into a tag's attribute (or other places where having quote marks would throw off parsing). In regular body text un-encoded will work fine.

<imgsrc="..."alt="A &quot;quote mark&quot; in an alt attribute" />

Solution 4:

No, you only need to use character references for quotes (single or double) if you want to use them inside an attribute value declaration that uses the same quotes for the value declaration:

title="The sign says &quot;Matt's Stuff&quot;"title='The sign says "Matt&#39;s Stuff"'

Both title values are The sign says "Matt's Stuff".

Post a Comment for "How To Encode Quotes In Html Body?"