kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow monks,

In the context of a user input from a textfield, how do you remove extra carriage returns and replace the remaining one with an html paragraph tag? To make it more concrete, the user enters sentence1, then hit Enter serveral times before inputting sentence2. You want to display the two sentences with a separated by a newline in an html format.

Thanks in advance :)

btw, the xp thingy has gone deranged.

Replies are listed 'Best First'.
Re: Removing extra carriage returns...
by dragonchild (Archbishop) on Apr 01, 2004 at 19:19 UTC
    It's up to you to figure out what it's actually doing.
    s!$/$/+!$/<br>!gm;

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Removing extra carriage returns...
by Anonymous Monk on Apr 01, 2004 at 20:36 UTC

    Perhaps something like this:

    use CGI::Simple; my $q = CGI::Simple->new(); my $txtarea = $q->param('usrInput'); $txtarea = '' unless( defined($txtarea) ); $txtarea =~ s#\r+##g; $txtarea =~ s#\n{2,}#<br />\n#g;
      Thanks!

      I modified yours to

      $html_str =~ s/(.*)\n{2,}(.*)/$1\n<p>$2<\/p>/g;
      Seems to work...Does it look right to you?
        Does it look right to you?

        Um, well, no, not really -- the AM's approach is better, because it just handles each occurrence of multiple line-breaks in a sensible, consistent way without affecting anything in between. Removing all the "\r" characters first is a nice touch, I think, and then replacing each string of two or more "\n" with a "br" tag does what is needed. (Basically all browsers would manage okay if you just used "<P>" instead of "<BR />", and if you like the results better with "p" tags, why not?)

        By contrast, your approach goes to the trouble of trying to capture stuff before and after the 2-or-more line-breaks, just so it can copy it all back with "p" tags around some of it. It's not hard to come up with some examples that would cause close-P tags to show up in the wrong places. Try running that on the following text (with single and double line-breaks present just as shown), and see what happens:

        First paragraph is easy, no matter whether it's one line or several. But a second paragraph with single line-breaks in it will mess you up, because "." in regexes won't match line breaks, and adding the "s" modifier on the regex to "fix" that will just break things worse.
Re: Removing extra carriage returns...
by qq (Hermit) on Apr 01, 2004 at 21:23 UTC

    btw, the xp thingy has gone deranged.

    341559