Rip's Domain

my XHTMLParagraphFormat update

Posted in ColdFusion by rip747 on February 15, 2007

As a saving point for myself, I though I would just make a post out of this update.

XHTMLParagraphFormat – converts all single returns into “<br/>” and all double returns into “<p>”. Can handle embedded markup (great for unsorted lists).

<!— XHTMLParagraphFormat —>
<cffunction name=”XHTMLParagraphFormat” access=”public” returntype=”Any” _returntype=”string” output=”false”
hint=”takes a string an converts it into valid XHTML 1.0 Strict markup”>
<cfargument name=”string” default=”” required=”false” type=”Any” _type=”string”>
<cfset var Local = StructNew()>
<!— return string —>
<cfset Local.string = “”>
<!— remove trailing and leading whitespace —>
<cfset arguments.string = trim(arguments.string)>
<!— All double hard returns with become <p> —>
<cfset arguments.string = ReplaceNoCase(arguments.string, “#Chr(13)##Chr(10)##Chr(13)##Chr(10)#”, “^”, “ALL”)>
<!— All single hard returns will become <br/> —>
<cfset arguments.string = ReplaceNoCase(arguments.string, “#Chr(13)##Chr(10)#”, “~”, “ALL”)>
<!— single hard returns followed by a “<” are usually embedded markup —>
<cfset arguments.string = ReplaceNoCase(arguments.string, “~<“, “^<“, “ALL”)>
<!— make grouped “^” into a single one —>
<cfset arguments.string = ReReplaceNoCase(arguments.string, “\^{2}”, “^”, “ALL”)>
<!— split into an array for wrapping —>
<cfset arguments.string = ListToArray(arguments.string, “^”)>
<!— loop through the array —>
<cfloop from=”1″ to=”#ArrayLen(arguments.string)#” index=”Local.i”>
<!— remove trailing and leading whitespace —>
<cfset arguments.string[Local.i] = trim(arguments.string[Local.i])>
<!— convert all single return into <br/> —>
<cfset arguments.string[Local.i] = ReplaceNoCase(arguments.string[Local.i], “~”, “<br/>”, “ALL”)>
<!— if it isn’t a tag —>
<cfif Left(arguments.string[Local.i], 1) NEQ “<“>
<!— wrap in <p></p> —>
<cfset arguments.string[Local.i] = “<p>” & arguments.string[Local.i] & “</p>”>
</cfif>
<!— combine for return —>
<cfset Local.string = Local.string & arguments.string[Local.i]>
</cfloop>
<cfreturn Local.string>
</cffunction>

If you have any other ideas for this, just leave a comment below.

One Response

Subscribe to comments with RSS.

  1. Peter Boughton said, on February 16, 2007 at 4:55 am

    You don’t need to use Chr() for newlines & carriage returns, and CRs shouldn’t be mandatory… try “\r?\n\r?\n” and “\r?\n”


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: