Clean and keep line breaks

Discussion in 'Programming' started by AimyThomas, Oct 14, 2012.

  1. #1
    Hello,

    I want to keep line spaces in comments but clean more then 2 after each.

    Example:

    $sentence="This is my first sentence
    This will be in a new row
    This will be too in a new row but not separated with 3line breaks just with

    one

    ";
    And i want to output just this:

    $sentence="This is my first sentence
    This will be in a new row

    This will be too in a new row but not separated with 3line breaks just with
    one
    ";
    Please Help

    Thanks in Advance
     
    AimyThomas, Oct 14, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    On a line by line basis, keep track of the number of lines you've read that have 0 length (after trimming off any \n). If there's zero or one, output a \n and the line and reset the counter. Adjust for the number of blank lines you'll accept. Once you've reached the "no more blanks", just keep reading in blank lines until you reach a non-blank line. Then reset the counter, output a \n and the line.

    (Replace \n with <br /> or whatever your newline is.)

    There are all sorts of ways to do this, but they all end up about the same. For instance, you can just ignore (don't output) blank lines if you don't want any at all.
     
    Rukbat, Oct 15, 2012 IP