CFLib.org – Common Function Library Project

trimHTML(str)

Last updated November 14, 2007

author

Kenric L. Ashe

Version: 1 | Requires: CF5 | Library: StrLib

Description:
Like Trim() except it also works on html whitespace i.e. <p></p>, <br>, <br/>, <br />, and non-breaking spaces.

Return Values:
Returns a string.

Example:

<cfset before = "<p>&nbsp;</p> <br> <p>this is a test&nbsp;<br /> &nbsp;</p>">
<cfset after = TrimHTML(before)>
before: <cfdump var="#before#">
<br><br>
after: <cfdump var="#after#">

Parameters:

Name Description Required
str String to format. Yes

Full UDF Source:

/**
 * Like Trim() except it also works on html.
 * 
 * @param str      String to format. (Required)
 * @return Returns a string. 
 * @author Kenric L. Ashe (cflib.org@kenric.com) 
 * @version 1, November 14, 2007 
 */
function trimHTML(str) {
    var htmlList = "<p>,</p>,<br>,<br/>,<br />,&nbsp;";
    var x = 0; var H = ""; var looping = 1;
    while (looping) {
        looping = 0;
        str = Trim(str);
        for (x=1; x lte ListLen(htmlList); x=x+1) {
            H = ListGetAt(htmlList, x);
            if (Left(str, Len(H)) is H) {str = Right(str, Len(str) - Len(H)); looping = 1;}
            if (Right(str, Len(H)) is H) {str = Left(str, Len(str) - Len(H)); looping = 1;}
        }
    }
    return 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