How to Enter Code in Blog Comments

Perhaps most people have no problem with leaving comments on blogs. But often for those wishing to post code examples, there are problems like the blog stripping out the code or showing a smilie :< :( Sometimes if the comment can't be edited, frustrated visitors will leave several comments in an attempt to get their code example to show. Here's a way to get around the security filtering that mangles code examples in comments.

Some blogs allow some HTML tags, some allow some BBcode tags, many don't allow any tags at all, some allow smilies. So what to do? The answer is to use entities.

If the code example contains HTML tags, replace all occurences of the left angle bracket - < - with it's entity - &#60; or &lt;

If the code example contains BBcode tags, replace all occurences of the left bracket - [ - with it's entity - &#91;

If the blog allows smilies, all occurences of the colon - : - should be replaced with it's entity - &#58;

And if the code already contains entitites, change the ampersands - & - to it's entity - &#38; or &amp;

For example, to leave a comment containing a code example like

[code]&rArr; Name:<input type="text" />[/code]

enter

&#91;code]&#38;rArr; Name&#58;&#60;input type="text" />&#91;/code]

Technorati Tags: , , ,

Post a Comment

Your email is never shared. Required fields are marked *

*
*