I recently came across a situation where I needed to strip all of the images out of an HTML string. What seemed like a daunting task was sped along with the help of some code from this site. For other people wanting to remove image <img> tags from strings, here is the solution.
<%@LANGUAGE=”VBSCRIPT” CODEPAGE=”1252″%>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Untitled Document</title>
</head>
<body>
<%
Dim StripString
StripString = “<p>I spent yesterday walking through <a href=”"http://www.armstrong.com/”" target=”"_blank”">Armstrong</a>’s newly Platinum-certified LEED-Existing Building corporate headquarters in Lancaster, Pennsylvania. Rick Fedrizzi, the USGBC’s president, spoke at the certification ceremony in the daylit central atrium.</p><p> <br /><img id=”"6acb2ee0-a329-4736-95a5-65ef554e86ad”" src=”"http://sitelife.construction.com/ver1.0/Content/images/store/10/5/6acb2ee0-a329-4736-95a5-65ef554e86ad.Large.jpg”" alt=”"blog post photo”" /><br /> </p><p>The building, originally designed and built by <a href=”"http://www.gensler.com/”" target=”"_blank”">Gensler</a> in 1998, looks out onto the rolling countryside, which is impossible to miss since there are gorgeous views from nearly every corridor, office, and stairwell in the structure.</p><p>A lot of us media types who were present for the building tour and ceremony asked various Armstrong executives how they planned to roll out LEED ratings to their other facilities and they were a little cagey. After all, greening a fancy corporate headquarters building is probably easier than a vinyl flooring manufacturing plant. </p><p>But I give Armstrong a lot of credit–after all, how many other corporations are greening their headquarters and certifying the results? Certainly not McGraw-Hill, the parent company of Architectural Record. But I would like to think they will, once more companies like Armstrong are leading the way.</p>”
%>
<%
Function stripIMG(strHTML)
‘Strips the HTML tags from strHTML using split and join
‘Ensure that strHTML contains something
If len(strHTML) = 0 then
stripHTML = strHTML
Exit Function
End If
dim arysplit, i, j, strOutput
arysplit = split(strHTML, “<img”)
‘Assuming strHTML is nonempty, we want to start iterating
‘from the 2nd array postition
if len(arysplit(0)) > 0 then j = 1 else j = 0
‘Loop through each instance of the array
for i=j to ubound(arysplit)
‘Do we find a matching > sign?
if instr(arysplit(i), “>”) then
‘If so, snip out all the text between the start of the string
‘and the > sign
arysplit(i) = mid(arysplit(i), instr(arysplit(i), “>”) + 1)
else
‘Ah, the < was was nonmatching
arysplit(i) = “<” & arysplit(i)
end if
next
‘Rejoin the array into a single string
strOutput = join(arysplit, “”)
‘Snip out the first <
strOutput = mid(strOutput, 2-j)
‘Convert < and > to < and > uncomment to display as HTML
’strOutput = replace(strOutput,”>”,”>”)
’strOutput = replace(strOutput,”<”,”<”)
stripHTML = strOutput
End Function
%>
<%
response.write stripHTML(StripString)
%>
</body>
</html>