Regular Expression: ASP Strip Tags
ASP, Regular Expressions February 16th, 2007This week, I have been mostly coding MAZIN. You will find out what that means when listening to Ram FM soon.
As part of the project, I had to create a page with a WYSIWYG editor (also known as Rich Text Editors) that would allow users to compose copy that may or may not include simple HTML tags such as bold, italic, lists, breaks and paragraphs.
As all sites we develop these days are XHTML based and standards compliant, I found that FCKEditor was the best choice - even though it is what Rich would call “Bloatware” - i.e, it’s rediculously large in terms of directories/language files/etc. That reminds me, I need to go through it and delete all the unwanted languages and plugin-in scripts before going live.
The problem with FCKEditor is that it will still allow users to post HTML that is not allowed i.e, you have told FCKEditor that you only want users to be able to make text bold, italic or whatever. This means that when we send the form, we need a strip_tags function, like PHP has.
Haven’t you already posted an ASP one!? I hear you ask. Well, I did. But the PHP version of strip_tags allows you to specify which tags you want to remain: www.php.net/strip_tags
After some researching, it seems that nobody has come up with an ASP version of this sweet function, so I wrote my own et voila:
Usage is simple, just do:
html = stripHTML(html, "b,i,strong,em,p,br")
Where b, i, strong, em, p and br are the tags you are allowing.
That’s all for now