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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson