Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    Regular Coder
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    111
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Clean and keep line breaks

    Hellou ... 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
    ";

    Any ideas? :P

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,225
    Thanks
    23
    Thanked 606 Times in 605 Posts
    You do not have line breaks in the php you have shown. You have white space, php does not see white space. And to php it looks like 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";

    Are you talking about something like this:
    $sentence="This is my first sentence.'<br />'.
    This will be in a new row.'<br /><br /><br />'.
    This will be too in a new row but not separated with 3line breaks just with.'<br /><br /><br />'.
    one.'<br /><br /><br />'
    ";

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Not sure if this is exactly what you are going for, but you can try something like this:
    PHP Code:
    $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



    "
    ;

    $clean trim(preg_replace('/[\r\n]{3,}/s'"\n\n"$sentence));

    var_dump($clean); 
    Outputs:
    Code:
    string(135) "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"
    Quote Originally Posted by sunfighter View Post
    You do not have line breaks in the php you have shown. You have white space, php does not see white space. And to php it looks like 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";
    That's not entirely true. While it is true that whitespace in between *code* is ignored, spaces/newlines/carriage returns inside of strings are not. If you echo that string wrapped in a <pre> tag or view the source of the page in your browser you would see the extra newlines OP is referring to. Granted, they would be collapsed when the page is rendered normally in the browser(not in a <pre> tag or <textarea>), but they are still part of the string inside of the variable and in the raw output.

  • #4
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    Quote Originally Posted by sunfighter View Post
    You do not have line breaks in the php you have shown. You have white space, php does not see white space. And to php it looks like 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";

    Are you talking about something like this:
    $sentence="This is my first sentence.'<br />'.
    This will be in a new row.'<br /><br /><br />'.
    This will be too in a new row but not separated with 3line breaks just with.'<br /><br /><br />'.
    one.'<br /><br /><br />'
    ";
    of course it does....
    if you don't believe, just run this:
    PHP Code:
    <?php
    $data 
    'line 1
    line 2
    line 3
    line 4
    line 5'
    ;
    $pieces explode("\n"$data);
    print_r($pieces);
    ?>
    as for original question: try this:
    PHP Code:
    <?php
    $data 
    'line 1
    line 2



    line 3



    line 4



    line 5'
    ;
    echo 
    preg_replace('#\n{2,999}#'"\n\n"$data);
    ?>
    this code will output this:
    Code:
    line 1
    line 2
    
    line 3
    
    line 4
    
    line 5
    ---edit---
    damn second again... lol
    Last edited by patryk; 10-14-2012 at 05:26 PM.

  • #5
    Regular Coder
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    111
    Thanks
    9
    Thanked 0 Times in 0 Posts
    [QUOTE=Inigoesdr;1280078]Not sure if this is exactly what you are going for, but you can try something like this:
    PHP Code:
    $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



    "
    ;

    [
    PHP]$clean trim(preg_replace('/[\r\n]{3,}/s'"\n\n"$sentence));

    var_dump($clean); 
    [/PHP]

    Outputs:
    Code:
    string(135) "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"
    Yes this "$clean = trim(preg_replace('/[\r\n]{3,}/s', "\n\n", $sentence));" was working good. But here is one more thing... Now the script works only when i add the css 'white-space' to the td box but how to delete the top white space? :P I dont know why its there.


  • #6
    Regular Coder
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    111
    Thanks
    9
    Thanked 0 Times in 0 Posts
    [QUOTE=Trki;1280104]
    Quote Originally Posted by Inigoesdr View Post
    Not sure if this is exactly what you are going for, but you can try something like this:
    PHP Code:
    $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



    "
    ;

    [
    PHP]$clean trim(preg_replace('/[\r\n]{3,}/s'"\n\n"$sentence));

    var_dump($clean); 
    [/PHP]

    Outputs:
    Code:
    string(135) "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"
    Yes this "$clean = trim(preg_replace('/[\r\n]{3,}/s', "\n\n", $sentence));" was working good. But here is one more thing... Now the script works only when i add the css 'white-space' to the td box but how to delete the top white space? :P I dont know why its there.

    and the white space at picture means that there is a white space before text when i do (inspect elemenet in chrome and check it i see white space before text) So i need somehow modify the function to cut the whitespace before text hm...

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,296
    Thanks
    13
    Thanked 345 Times in 341 Posts
    just because there is white space does not mean there is whitespace. you can get that as well with margins or paddings without the use of whitespace at all. wee would need to see the HTML/CSS code for that to be clear.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #8
    Regular Coder patryk's Avatar
    Join Date
    Oct 2012
    Location
    /dev/couch
    Posts
    398
    Thanks
    2
    Thanked 64 Times in 64 Posts
    try this:
    PHP Code:
    $data '    
        
             
    line       1
    line            2

           
                              
    line       3
         
    line            4


    line 5

        '
    ;
    $data preg_replace('#[\r\n]+#'"\n"$data); //just in case...

    $data =  preg_replace('#^\s+\n#'''$data); //strip first line if contains only spaces, tabs etc...

    $data =  preg_replace('#[\t ]+#'' '$data); //strip multiple spaces and tabs

    $data =  preg_replace('#\s+$#'' '$data); //strip whitespaces and \n's ending string

    echo preg_replace('#(\n| \n){2,}#'"\n\n"$data); 
    works on my server
    Last edited by patryk; 10-15-2012 at 02:05 PM.

  • #9
    Regular Coder
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    111
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by patryk View Post
    try this:
    PHP Code:
    $data '    
        
             
    line       1
    line            2

           
                              
    line       3
         
    line            4


    line 5

        '
    ;
    $data preg_replace('#[\r\n]+#'"\n"$data); //just in case...

    $data =  preg_replace('#^\s+\n#'''$data); //strip first line if contains only spaces, tabs etc...

    $data =  preg_replace('#[\t ]+#'' '$data); //strip multiple spaces and tabs

    $data =  preg_replace('#\s+$#'' '$data); //strip whitespaces and \n's ending string

    echo preg_replace('#(\n| \n){2,}#'"\n\n"$data); 
    works on my server
    didnt help man ... still this:


  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,296
    Thanks
    13
    Thanked 345 Times in 341 Posts
    maybe you’re not trimming the right string?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #11
    Regular Coder
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    111
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    maybe you’re not trimming the right string?
    ah yes i am because i made there a lot of spaces and in trimed to max one which is gr8 it just didnt cleared the white space from forward of the string how u can see

  • #12
    Regular Coder
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    111
    Thanks
    9
    Thanked 0 Times in 0 Posts
    hm.. so nobody know how to fix the white spat there? :P

  • #13
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Where are you outputting it at? trim() removes the whitespace from both ends of the string.

  • #14
    Regular Coder
    Join Date
    Jan 2012
    Location
    Slovakia
    Posts
    111
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    Where are you outputting it at? trim() removes the whitespace from both ends of the string.
    aaaaaah i fixed it.. there was problem not with function... but there were white spaces directly in code between tags... i didnt realize that it can be problem. Thx to all of you and sorry for my mistake in last point :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •