Detecting New Lines

Discussion in 'PHP' started by Pudge1, Jan 12, 2010.

  1. #1
    I have a text file that looks like this:

    It
    is like
    this
    and this
    and like this lalala
    text

    Well I am using file_get_contents(); and then displaying it as HTML. I want to use str_replace() to replace the new lines. What would I replace. I tried /n but it didn't work. What should I try replacing?
     
    Pudge1, Jan 12, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Don't you mean \n? :)

    Use this:

    
    <?php
    
    $content = <<<CONTENT
    It
    is like
    this
    and this
    and like this lalala
    text
    CONTENT;
    
    $content = str_replace("\n","", $content);
    
    highlight_string($content);
    ?>
    
    PHP:
     
    danx10, Jan 12, 2010 IP
  3. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    That is what I meant. I used that before and it doesn't work.
     
    Pudge1, Jan 12, 2010 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $content = preg_replace('%[\r\n]+%', '', $content);
    PHP:
     
    JAY6390, Jan 12, 2010 IP
  5. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #5
    Thanks! Works.
     
    Pudge1, Jan 12, 2010 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No problem :)
     
    JAY6390, Jan 12, 2010 IP
  7. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #7
    It detects n's and r's as new lines though haha. Have a fix for that?
     
    Pudge1, Jan 12, 2010 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    It doesn't. Can you show your code?
    $content = "Hello 
    This is a test to 
    see if n and r get removed 
    from a file using regex";
    
    $result = preg_replace('%[\r\n]+%', '', $content);
    echo $result;
    
    PHP:
    That code shows perfectly
     
    JAY6390, Jan 12, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Hmm, so did mine -> http://forums.digitalpoint.com/showpost.php?p=13351017&postcount=2

    I tested, so would help if op displayed his code.
     
    danx10, Jan 13, 2010 IP
  10. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #10
    
    @$note_contents = preg_replace('%[\r\n]+%', '<br />', $note_contents);
    
    echo '<div align="center"><b>' . $note_title . '</b><br />By: ' . $note_author . '</div><br /><br />';
    echo $note_contents;
    
    Code (markup):
    It replaces it with a new line new lines but also n's and r's
     
    Pudge1, Jan 13, 2010 IP
  11. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #11
    If you just want to replace new lines with br why not just use
    $note_contents = nl2br($notecontents);
    PHP:
     
    JAY6390, Jan 13, 2010 IP