Preserve linebreak txt php

前端 未结 2 378
余生分开走
余生分开走 2021-01-03 10:46

How can I read a .txt file from my server, and preserve it\'s linebreaks? Note that the linebreaks aren\'t like this /n or something, they are more

相关标签:
2条回答
  • 2021-01-03 11:30

    The line breaks are preserved without you needing to do anything. You can easily verify this by running the code from the command line, for example with

    php -r "readfile('text.txt');"
    

    However, in HTML whitespace is collapsed by default. If you want to preserve it use the CSS white-space attribute like this:

    <div style="white-space: pre"><?php readfile('text.txt'); ?></div>
    
    0 讨论(0)
  • 2021-01-03 11:32

    For output? Just use nl2br

    $file = file_get_contents( 'file.txt' );
    echo nl2br( $file );
    

    Also works with fopen.

    0 讨论(0)
提交回复
热议问题