Preg_Replace newlines

Discussion in 'PHP' started by alphacooler, Jul 26, 2006.

  1. #1
    I have a form box where users enter in directions (one per line). I then turn each line into a list item <li>. The problem is that when people enter a blank line at the end it creates a new list item.

    So I tried the following preg_replace to strip out extra lines at the end of the string. It doesn't work.

    $directions = preg_replace('/\r+$/','',$_POST['directions']);
    Code (markup):
    I also tried \n instead of \r.
     
    alphacooler, Jul 26, 2006 IP
  2. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #2
    HEhe, you should combine both!
    THis should works

    
    $directions = preg_replace('/[\r\n]+$/','',$_POST['directions']);
    
    PHP:
     
    goldensea80, Jul 26, 2006 IP
  3. blueoceanwave

    blueoceanwave Peon

    Messages:
    210
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Here's an example, let me know how this works:

    
    <?php
    // string with bunch of CRLF's
    $my_string = "Liquid\r\nTension Experiment\r\n\r\n\r\n";
    
    // replace CRLF's with spaces
    $my_wonderful_string = str_replace("\r\n", " ", $my_string);
    // would result in "Liquid Tension Experiment  "
    
    // or just delete the CRLF's (by replacing them with nothing)
    $my_wonderful_string = str_replace("\r\n", "", $my_string);
    // would result in "LiquidTension Experiment"
    ?>
    PHP:
     
    blueoceanwave, Jul 26, 2006 IP
  4. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #4
    Sorry but I think you miss the topic starter's point. You cannot remove the in-string \r\n. And have you tested my simple code?
     
    goldensea80, Jul 27, 2006 IP
  5. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Why not just use rtrim?

    
    rtrim($string);
    
    PHP:
    Thomas
     
    coderlinks, Jul 27, 2006 IP
    goldensea80 likes this.
  6. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think what you want to do is remove any trailing spaces and/or newlines and/or tabs and then see how long the string is. When you encounter a zero length string, ignore it. Otherwise, put it inside <li></li> tags.

    Also, you are right to use preg_replace instead of str_replace because it allows you to test for combinations of characters, instead of assuming they will a set type.

    As a side note, I have some people say using isset($directions[0]) to find out if $directions has content works faster than strlen($directions).
     
    clancey, Jul 27, 2006 IP
  7. goldensea80

    goldensea80 Well-Known Member

    Messages:
    422
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    128
    #7
    OH, that's works. People seems to be likely the complicate the problem...And I am one one them. Thanks for "opening my eyes" ;)
     
    goldensea80, Jul 28, 2006 IP