ASP Code

Mimic PHP's strip_tags() function with ASP

PHP has a great function called 'strip_tags'. You pass it a string of HTML, and it removes all formatting and tags, and returns only the text portion. Here's the equivalent in ASP, using regular expressions to do the finding and replacing of HTML tags.


'====
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= "<&#91;^>]+>"
        strToStrip = objRegExp.Replace(strToStrip, "")
        Set objRegExp = Nothing
    End If
    StripHTMLTags =  strToStrip
End Function
Share

4 Comments

  1. Thanks alot for developing these types of codes in order to save the WEB DEVELOPERS time. This is very helpful for us from you.

    Please try to find as much function as present in PHP or ASP but not available in.

    '====
    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

  2. In ASP.NET you could use a function that I developed. The code can be found at CodeProject here

  3. Have a look at my ASP.NET implementation at http://www.codeproject.com/KB/MCMS/htmlTagStripper.aspx

    Thank you! 😉

  4. I just found this function, three years later.

    Nice function, but it does have a problem.

    You will want to replace StripHTMLTags with StripTags

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image