Mimicking PHP's String Formatting Functions - strip_tags
(Page 3 of 4 )
PHP brings a great function to the table called 'strip_tags'. Through it we can pass a string, and out the other side pops out the stripped-down shadow of its former self. This would be extremely useful in a forum or chat area, where people are constantly trying to throw in some HTML formatting.
There are a couple of ways we could approach this. One would be to flow through the entire string, look for any '<' brackets, and methodically pull away the information around the tags into chunks that can be pieced together separately.
Needless to say, this is a very laborious approach to solving the issue. How much nicer would it be to just use Regular Expressions? A lot, so let's do it! If you need to know more about regular expressions, here's a great tutorial: http://www.devarticles.com/c/a/ASP/The-Complete-Regular-Expression-Guide. But for now we'll use a simple one, just looking for the common brackets that enclose a tag. Here's the function:
'===================================================
Function stripTags( strToStrip )
'===================================================
Dim objRegExp
strToStrip = Trim( strToStrip & "" )
If Len( strToStrip ) > 0 Then
Set objRegExp = New RegExp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern= "<[^>]+>"
strToStrip = objRegExp.Replace(strToStrip, "")
Set objRegExp = Nothing
End If
StripHTMLTags = strToStrip
End Function
So as you read through the lines of code, you should be able to figure it out. We build the regular expression to find any tags, and blow through the entire string replacing the tags with “”. How handy! Now let's move on to the last.
Next: str_pad >>
More ASP Articles
More By Justin Cook