View comments | RSS feed

ParagraphFormat

Description

Replaces characters in a string:

Returns

A copy of the string, with characters converted.

Category

Display and formatting functions, String functions

Function syntax

ParagraphFormat(string)

See also

StripCR

Parameters

Parameter Description

string

A string or a variable that contains one

Usage

This function is useful for displaying data entered in textarea fields.

Example

<h3>ParagraphFormat Example</h3>
<p>Enter text into this textarea, and see it returned as HTML.
<cfif IsDefined("FORM.myTextArea")>
   <p>Your text area, formatted
   <p><cfoutput>#ParagraphFormat(FORM.myTextArea)#</cfoutput>
</cfif>
<!--- use #Chr(10)##Chr(13)# to simulate a line feed/carriage
return combination; i.e, a return --->
<form action = "paragraphformat.cfm">
<textArea name = "MyTextArea" cols = "35" ROWS = 8>
This is sample text and you see how it scrolls
<cfoutput>#Chr(10)##Chr(13)#</cfoutput>
From one line
<cfoutput>#Chr(10)##Chr(13)##Chr(10)##Chr(13)#</cfoutput>
to the next </textArea> <input type = "Submit" name = "Show me the HTML version"> </form>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


pete_freitag said on Apr 26, 2005 at 1:50 PM :
Note that the ParagraphFormat tag inserts P tags not p tags, note the case. This function is not XHTML safe.
No screen name said on May 7, 2006 at 8:26 PM :
For the <P> problem with XHTML, I used this work-around: Just replace the result from #paragraphformat(variable)# with #replace(variable, '<P>', '<br><br>', ALL)#

This is a hack of a workaround, but it solves the XHTML problem
rubblemasta said on May 15, 2006 at 8:35 AM :
Or you can always wrap ParagraphFormat() in a Replace function to fix the rogue <P> and </P> instances.
sthompson said on Apr 10, 2007 at 10:23 AM :
Further, in regards to XHTML, the function only inserts opening <P> tags. There are no closing tags as required by XHTML.

Why does the function assume that single newlines aren't wanted? I think that single newline characters should be preserved, and replaced with <br />. Double newlines could be replaced with two <br /> tags. That fixes the XHTML problem.
Daniel Stockman said on Sep 6, 2007 at 4:24 PM :
Cfscript one-liner that does the job:

function FormatParagraph(txt) { return ('<p>' & ReReplace( ReReplace(
Trim(txt), '\r\n\r\n', '</p><p>', 'all'), '\r\n', '<br />', 'all') & '</p>'); }
halL said on Sep 7, 2007 at 9:57 AM :
The solution suggested by Hoska results in XHTML-compliant code, but puts its output all on one line.
The following version puts line breaks to aid source readability.
It also puts a line break in if there is a single \r\n.
It is arguable whether any of these versions is the "right" solution; I suspect it depends on your circumstances.

Finally, note that the example on the page has a bug: the <form> tag needs a method="POST" attribute.

function FormatParagraph(txt) {
var temp = ReReplace(Trim(txt), '\r\n\r\n', '</p><p>', 'all');
temp = '<p>' & ReReplace(temp, '\r\n', '<br />', 'all') & '</p>';
temp=Replace(temp, '</p>', '</p>#Chr(10)##Chr(13)#', 'all');
temp=Replace(temp, '<br />', '<br />#Chr(10)##Chr(13)#', 'all');
return temp;
}

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000592.htm