CFLib.org – Common Function Library Project

stripHTML(string)

Last updated May 10, 2016

author

Raymond Camden

Version: 5 | Requires: CF5 | Library: StrLib

Description:
Returns the string with all HTML removed. Unlike HTMLEditFormat which escapes HTML, this function actually removes the HTML.

Return Values:
Returns a string.

Example:

<cfset str="the <B>dog</B> jumped over the <A HREF=""foo.html"">fox</A>.">
<cfoutput>
Given str=#str#<BR>
The StripHTML version is #StripHTML(str)#
</cfoutput>

Parameters:

Name Description Required
string String to be modified. Yes

Full UDF Source:

/**
 * Removes HTML from the string.
 * v2 - Mod by Steve Bryant to find trailing, half done HTML.
 * v4 mod by James Moberg - empties out script/style blocks
 * v5 mod by dolphinsboy
 * 
 * @param string      String to be modified. (Required)
 * @return Returns a string. 
 * @author Raymond Camden (ray@camdenfamily.com) 
 * @version 4, October 4, 2010 
 */
function stripHTML(str) {
    // remove the whole tag and its content
    var list = "style,script,noscript";
    for (var tag in list){
        str = reReplaceNoCase(str, "<s*(#tag#)[^>]*?>(.*?)","","all");
    }

    str = reReplaceNoCase(str, "<.*?>","","all");
    //get partial html in front
    str = reReplaceNoCase(str, "^.*?>","");
    //get partial html at end
    str = reReplaceNoCase(str, "<.*$","");

    return trim(str);

}

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson