For example:
A user submits a form with a <textarea name="mytext" cols="35" rows="2"></textarea>
and presses ENTER within it. How would I replace the CR-LF with a <br />
?
CF has a function for this called ParagraphFormat():
<cfset form.userText = paragraphFormat(form.usertext)/>
From the help docs -
Replaces characters in a string:
<p>
)It may do more than you want in that it also looks for double line breaks and adds <p>
and </p>
tags.
Ben also has an enhanced version (a UDF) called paragraph2 that would be easy to modify to get the exact affect you want. Here's the link:
http://www.cflib.org/udf/ParagraphFormat2
<cfset localVars.ReturnString = REReplace(localVars.ReturnString, "\r\n|\n\r|\n|\r", "<br />", "all")>
You shouldn't hit \n\r
naturally, but it can happen if it's being inserted by a dev who has forgotten the correct order.
That's a subset of a more generalised function to replace end of lines (EOL) chars with something else based on what you're doing (e.g. having to write out in Windows/Linux format, .ics files, html, cfheaders, etc)
<cffunction name="ReplaceEOL" access="public" output="false" returntype="string" hint="Replaces EOL codes with other characters">
<cfargument name="String" required="true" type="string">
<cfargument name="ReplaceWith" required="true" type="string">
<cfreturn REReplace(Arguments.String, "\r\n|\n\r|\n|\r", Arguments.ReplaceWith, "all")>
</cffunction>
Instead of replacing it with br I would use the ParagraphFormat function when displaying the values.
You can use the paragraphFormat() function but sometimes a replace function helps you visualize what is actually happening.
An example: <cfset TheText=replace("#form.myText#",chr(13)&chr(10),"<br />","all")>
This replaces all carriage return-line feeds with an html line break
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With