[thelist] PHP: How to detect a line break in a textfield

Joshua Olson joshua at waetech.com
Tue Feb 25 08:50:02 CST 2003

----- Original Message -----
From: "Chris Marsh" <chris at ecleanuk.com>
Sent: Tuesday, February 25, 2003 9:36 AM

> > If the user is allowed to enter simple HTML in the text area,
> > such as ordered-lists, how could you adapt this strategy to
> > not insert <br /> between list elements?
> If I had a gun at my head I'd move swiftly to the joys of regular
> expressions. If not, I would not allow it.

The reason I asked is because I've recently encountered this sort of
scenario.  Also, the user tortured me by putting spaces on the blank lines.
Ugh.  The whole paragraph replacement for double CRLF failed miserably.

I ended up using Regex to replace CRLF+<whitespace>+CRLF with CFLF+CRLF
prior to running the paragraph replacement.  But, I didn't have the budget
to work out a regex to stop the introduction of <br /> between html tags
such as ordered lists, table cells, etc.

Anybody care to take a stab at creating a fairly robust set of replacements
to convert textarea entered text into properly formatted paragraph+br text?

This was the set I used, though I'm sure it can be improved upon:

<!--- Replace multiple spaces with a single space --->
<cfset page_text = REReplace(page_text, "[ ]+", " ", "ALL")>

<!--- Replace CRLF followed by multiple spaces with a single space CRLF --->
<cfset page_text = REReplace(page_text, "#Chr(13)##Chr(10)#[ ]*", Chr(13) &
Chr(10), "ALL")>

<!--- Trim all spaces from the front of the text (I guess I could of used
Trim??) --->
<cfset page_text = REReplace(page_text, "^[ ]*", "", "ALL")>

<!--- Replace double CRLF with paragraph tags --->
<cfset page_text = Replace(page_text, Chr(13) & Chr(10) & Chr(13) & Chr(10).
"</p><p>", "ALL")>

<!--- Replace remaining single space CFLF with <br /> --->
<cfset page_text = Replace(page_text, Chr(13) & Chr(10), "<br />", "ALL")>

<!--- If you wanted it to be cosmetic then I guess you could insert CRLF
after </p> and <br /> --->



More information about the thelist mailing list