Php new line?!?

Discussion in 'PHP' started by webber09, Sep 8, 2010.

  1. #1
    hi everyone... i am writing a php to file write script. i have the info entering from a form on a separate page. below is the code i have written however i need to get the info to be entered on a new line each time its entered.

    <?
    
    $name = $_POST['name'];
    $message = $_POST['message'];
    
    $fp = fopen('./messages.txt', 'a');
    fwrite($fp, '' . $name . ': ' . $message . '');
    fclose($fp);
    ?>
    Code (markup):
    where abouts can i enter the new line command and what is it?

    Thanks =]
     
    webber09, Sep 8, 2010 IP
  2. themullet

    themullet Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #2
    newline is "\n"
     
    themullet, Sep 8, 2010 IP
  3. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #3
    i know its a \n but where on my script do i put it... i have tried it everywhere
     
    webber09, Sep 8, 2010 IP
  4. Eager2Seo

    Eager2Seo Member

    Messages:
    72
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #4
    fwrite($fp, '' . $name . ': ' . $message . '\n');
    Code (markup):
    I also prefer for html:

    echo "<br />";
    Code (markup):
    I have it saved as a one key macro in my editor.

    To add: windows may need \r\n

    Some text editors may not be able to show the line break, however...for obvious reasons.
     
    Last edited: Sep 8, 2010
    Eager2Seo, Sep 8, 2010 IP
  5. webber09

    webber09 Active Member

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    71
    #5
    thank you Eager2Seo,

    i have now changed it to add to a .html file rather than a .txt file and used the <br /> rather than \n.

    now i have done this the page that includes the .html file reads the <br /> and puts the new lines in

    again thank you very much :D
     
    webber09, Sep 8, 2010 IP
  6. Eager2Seo

    Eager2Seo Member

    Messages:
    72
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #6
    :cool: No problem.

    The / in the tag makes it XHTML compliant as well.
     
    Eager2Seo, Sep 8, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    You need to wrap double quotes around the \n, so it evaluates.

    Also for cross-server compatability consider using \r\n
     
    danx10, Sep 8, 2010 IP