small php issue regards readfile and convert "\n" to <br />

Discussion in 'PHP' started by Greyhound, Feb 8, 2011.

  1. #1
    hello i met some problem when i use readfile function. i have a text file a.txt with the following content:

    1234567
    abcdefg
    ABCDEFG

    and i used readfile("a.txt") to read the file and print the content on the screen but i got:

    1234567 abcdefg ABCDEFG

    but when i read the source file in browser it is:

    1234567
    abcdefg
    ABCDEFG

    so i suppose the browsers explain signal "\n" as a blank space and i should convert those "\n" to <br /> in the output html file. so i used nl2br function to do it:

    nl2br(readfile("a.txt"));

    but, still i got same issue, without seeing any <br /> in the browser source file. so i am really confused. anyone who can tell me how to get the correct result? thanks.
     
    Greyhound, Feb 8, 2011 IP
  2. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    str_replace("\n", "<br>", $string);
     
    moads, Feb 8, 2011 IP
  3. Greyhound

    Greyhound Banned

    Messages:
    1,637
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    78
    #3
    still the same resualt.
     
    Greyhound, Feb 8, 2011 IP
  4. museikaze

    museikaze Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Not 100% sure what you ment but is this what your looking for?

    $filename = "file.txt";
    $handle = fopen($filename, "r");
    $contents = fread($handle, filesize($filename));
    fclose($handle);

    $contents = explode('/n', $contents);

    for ($content in $contents){
    print $content . "<br />";
    }
     
    museikaze, Feb 8, 2011 IP