I've gone through and read all the other posts in reply to this, and they
all seem to ignore a very simple solution.

First: strip off the \r\n:
s/\r\n/\n/sg

Then look for the pattern \n\n (which would indicate the existence of an
empty line. For example: "Some paragraph text\n\nA new paragraph") and
replace it with \n<p>\n (which gives you "Some paragraph text\n<p>\nA new
paragraph"):
s/\n\n/\n<p>\n/sg

In both regular expressions, you treat the text as a single string (s)
and replace all instances (g).

The problem with using patterns such as s/$/<p>/m is that while it will
match the end-of-line condition, it will not _replace_ it (see
Programming Perl, chapter 2, on Regular Expressions and the s///
operator).

--Matthew

On Mon, 20 May 2002 13:51:57 -0400, John Brooking wrote:
> Hello, all,
> 
>    I'm trying to translate the value entered in a
> TEXTAREA tag to one or more HTML paragraphs. That means any newlines
> entered into the text box need to be turned into P tags by the script.
> But I'm having trouble coming up with a regex to do this. I know I need
> multiline mode, so I've been trying combinations involving "s/$/<p>/mg"
> or "s/^/<p>/mg", but nothing has worked so far. Everything I've tried
> has (1) added the P tag but not removed the newline, and/or (2) also
> added one at the end of the string (using $, or at the beginning using
> ^) even though there's not a newline there. The latter seems to be by
> Perl design, but I don't want it in this case.
> 
>   Here's the test program I'm using to experiment
> (warning to HTML email clients, there is an HTML tag in this code):
> 
>     my $lf = chr(10);
>     my $Text = "Para.${lf}Para.${lf}Para."; $Text =~ s/$/<p>/mg; print
>     $Text;
> 
>   And a follow-up question: When newlines are entered
> into a TEXTAREA, is TEXTAREA standardized to use CR/LF pairs, or just
> CR, just LF, or does it depend on the client and/or the server platform?
> Do we need to worry about this? Or will the ^ and $ characters work
> correctly with any of these combinations so we don't need to worry about
> it?
> 
> Thanks in advance for any help.
> 
> - John
> 
> 
> =====
> "When you're following an angel, does it mean you have to throw your
> body off a building?" - They Might Be Giants, http://www.tmbg.com ----
> Word of the week: Serendipity, see
> http://www.bartleby.com/61/93/S0279300.html
> 
> __________________________________________________ Do You Yahoo!? LAUNCH
> - Your Yahoo! Music Experience http://launch.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to