How do I remove extra line breaks beyond two?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • revjjjames@hotmail.com

    How do I remove extra line breaks beyond two?

    Hello -

    What is the correct expression to remove any extra line breaks beyond
    two in a string?

    Sincerely,

    Jim

  • Andy Hassall

    #2
    Re: How do I remove extra line breaks beyond two?

    On 25 Feb 2005 15:25:17 -0800, revjjjames@hotm ail.com wrote:
    [color=blue]
    >What is the correct expression to remove any extra line breaks beyond
    >two in a string?[/color]

    There any may ways but I'd probably use something like:

    $string = preg_replace('/(?<=\n)(\n?)\n*/s', "$1", $string);

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Chung Leong

      #3
      Re: How do I remove extra line breaks beyond two?

      <revjjjames@hot mail.com> wrote in message
      news:1109373917 .583770.16760@g 14g2000cwa.goog legroups.com...[color=blue]
      > Hello -
      >
      > What is the correct expression to remove any extra line breaks beyond
      > two in a string?
      >
      > Sincerely,
      >
      > Jim
      >[/color]

      The simpliest way is to replace linebreaks of three or more with two. Ugly
      regexp:

      preg_replace('/(?:(?:\r\n)|\r| \n){3,}/', "\n\n", $str);


      Comment

      Working...