1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Stripping HTML from an ASP code and then Trimming It

Discussion in 'C#' started by jimmykup, Sep 20, 2010.

  1. #1
    Hello,

    To start off I would like to say that I have very little experience in dealing with classic ASP. My specialty is HTML and CSS so I'm a little bit lost here. Hopefully someone here can lend me a hand.

    My Goal:
    My company sells projectors and other AV equipment. I would like to take the product descriptions that appear on the individual product pages and use them in my META tags and Facebook OpenGraph tags.

    The Problem:
    The product descriptions are saved in our database as text with HTML tags in it. I need these descriptions to load into my HTML as plain text without HTML elements or styling.

    Here's what I would like to do:
    1. Strip HTML tags from a body of text.
    2. Trim that text to a pre-determined length or after a certain amount of sentences.

    Here is the ASP tag in my HTML that loads the product description.

    <%=sDescription%>
    Code (markup):
    I searched for a code that would help me with removing the HTML tags and I found this piece right here.

    
    Function stripTags(HTMLstring)
        Set RegularExpressionObject = New RegExp
        With RegularExpressionObject
            .Pattern = "<[^>]+>"
            .IgnoreCase = True
            .Global = True
        End With
        stripTags = RegularExpressionObject.Replace(HTMLstring, "")
        Set RegularExpressionObject = nothing
    End Function
    
    Code (markup):
    But I don't know how to get that code to interact with my code. I'm familiar with Regular Expressions so I understand how this code does what it does. Unfortunately I don't understand anything else about it. I've tried replacing "HTMLstring" with my code but that didn't do anything.

    If this code isn't enough, there's plenty of more code in my source that I can supply you with. Just tell me what I should be looking for. Any help would be greatly appreciated, thank you! :)
     
    jimmykup, Sep 20, 2010 IP
  2. camjohnson95

    camjohnson95 Active Member

    Messages:
    737
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #2
    At the top of the ASP page add the code for the function (I have also added a function to shorten the text if too long):
    
    <%
    Function stripTags(HTMLstring)
        Set RegularExpressionObject = New RegExp
        With RegularExpressionObject
            .Pattern = "<[^>]+>"
            .IgnoreCase = True
            .Global = True
        End With
        stripTags = RegularExpressionObject.Replace(HTMLstring, "")
        Set RegularExpressionObject = nothing
    End Function
    
    'shorten to length
    Function shorten(strText, intLength)
    	If Len(strText) > intLength Then
    		shorten = Left(strText, intLength)
    	Else
    		shorten = strText
    	End If
    End Function
    %>
    
    Code (markup):
    Now to apply this to the product description:
    
    <%=shorten(stripTags(sDescription),200) %>
    
    Code (markup):
    Where 200 is the maximum number of characters that you want to display..

    Hope this helps.
     
    camjohnson95, Sep 22, 2010 IP