SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Trimming and Stripping HTML from an ASP code

    Hello,

    I have this ASP tag in my HTML at this site.

    Code:
    <%=sDescription%>
    When the page loads it pulls information from our database and inserts it into the HTML. The information that is pulled is a description of one of the products that we sell. Unfortunately, the information is stored in our database with the full text and formatting included with it. Like <p> tags for instance.

    I'd like to be able to pull this information from the database and then strip it of all it's HTML and styling tags so that it's just plain text.

    Is this possible with ASP? And if it is, can I also then dynamically have ASP trim that plain text to a set amount of characters?

    Any help would be greatly appreciated, thank you!

  2. #2
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes it's possible, and there are loads of references online.

    http://www.google.co.uk/search?q=asp+strip+html+tags

    Use the search, Luke. Use the search. /Star Wars
    Ian Anderson
    www.siteguru.co.uk

  3. #3
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply.

    That's actually one of the first things I did, but I couldn't make any sense of the results. Like this, for example, seems to be the popular result.

    Code:
    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
    But I don't know how to get that code to interact with my code. I've tried replacing "HTMLstring" with my code but that didn't do anything.

  4. #4
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Using that function as the method to get rid of HTML from what you want to display ...

    Code:
    <%
    sDescription = "Some <b>data with</b> HTML in it, comes from the database"
    sDescription = stripTags(sDescription)
    %>
    
    <!-- Yada yada other usual HTML code here -->
    
    <%=sDescription%>
    Ian Anderson
    www.siteguru.co.uk

  5. #5
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the follow-up.

    My lack of knowledge with ASP is really showing here I guess.

    Here is all of the code that I believe is involved in this "sDescription" code that generates the Product Description on this page: http://www.iavi.com/itemdesc_social4.asp?ic=plvz4000

    Code:
    <&#37;End if
    If Not rsItems.Eof Then
    sDescription = rsItems("WEBDESC")
    If Not rsParent.Eof Then%>
    
    <%=sDescription%>

    I tried using all of the code you've suggested here but it isn't working for me. Here is the code as I have implemented it.

    Code:
    <%Function stripTags(sDescription)
        Set RegularExpressionObject = New RegExp
        With RegularExpressionObject
            .Pattern = "<[^>]+>"
            .IgnoreCase = True
            .Global = True
        End With
        stripTags = RegularExpressionObject.Replace(sDescription, "")
        Set RegularExpressionObject = nothing
    End Function%>
    
    <%
    sDescription = stripTags(sDescription)
    %>
    
    <%=sDescription%>

    The end result is no different than it was before. I'd like to be able to load the data with HTML in one area, and without HTML in another area. I feel like I'm so close but I'm just missing something.

  6. #6
    SitePoint Wizard siteguru's Avatar
    Join Date
    Oct 2002
    Location
    Scotland
    Posts
    3,631
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Obviously I don't have your database, so I can't know what content sDescription holds when read from the database. Below is the code I tested before making my previous reply.
    Code ASP:
    <%
    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
     
    sDescription = "Some <b>data with</b> HTML in it, comes from the database"
    sDescription2 = stripTags(sDescription)
    %>
     
    <p><%=sDescription%></p>
    <p><%=sDescription2%></p>
    Using another variable name (sDescription2 in this case) allows both versions to coexist.
    Ian Anderson
    www.siteguru.co.uk

  7. #7
    SitePoint Member
    Join Date
    Sep 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Haha!

    I just can't get this code to work for me at all. I don't know why. But I searched around and found this and it's finally working!

    Code:
    <&#37;End if
    	If Not rsItems.Eof Then
    		sDescription = rsItems("WEBDESC")
    		If Not rsParent.Eof Then%>
    
    <%Function stripHTML(strHTML) 
    	Dim objRegExp, strOutput
    	Set objRegExp = New Regexp
    	objRegExp.IgnoreCase = True
    	objRegExp.Global = True
    	objRegExp.Pattern = "<(.|\n)+?>"
    	strOutput = objRegExp.Replace(strHTML, "")
    	strOutput = Replace(strOutput, "<", "&lt;")
    	strOutput = Replace(strOutput, ">", "&gt;")
    	stripHTML = strOutput 'Return the value of strOutput
    	Set objRegExp = Nothing
    End Function%> 
    
    <%=stripHTML(sDescription)%>
    
    <%End if%>
    You can see the text minus the stripped HTML at the top of this page. http://www.iavi.com/itemdesc_social4.asp?ic=plcxp200l

    Now I just need to figure out how to trim this down to two sentences or so and I'm set.

    Thanks for your help.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •