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.

In reply to Re: Re: Re: Removing extra carriage returns... by graff
in thread Removing extra carriage returns... by kiat

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.