5

I have a PHP script which retrieves the contents of a raw / plain-text file and I'd like to output this raw text in HTML, however when it's outputted in HTML all the line breaks don't exist anymore. Is there some sort of PHP function or some other workaround that will make this possible?

For example, if the raw text file had this text:

hello
my name is

When outputted in HTML, it will say:

hello my name is

Is there any way to preserve these line breaks in HTML? Thank you.

(If it helps, my script gets the contents of the raw text file, puts it inside a variable, and I just echo out the variable.)

Chrispy
  • 311
  • 3
  • 6
  • 12

4 Answers4

9

There are several ways to do this:

Using file_get_contents() and nl2br():

echo nl2br( file_get_contents( 'filename.txt'));

This won't solve special entities like <>&, you'll need to use htmlspecialchars()

echo nl2br( htmlspecialchars( file_get_contents( 'filename.txt')));

Perhaps better solution would be loading entire file into array with file() and iterate trough all elements (you'll be able to put lines into table or so in future)

$data = file( 'filename.txt');
foreach( $data as $line){
    echo htmlspecialchars( $line) . '<br />';
}

And if you need to process large amount of data it'd be best to do it sequentially with fopen() and fgets():

$fp = fopen( 'filename.txt', 'r') or die( 'Cannot open file');
while( $line = fgets( $fp)){
    echo htmlspecialchars( $line) . '<br />';
}
fclose( $fp);
Vyktor
  • 20,559
  • 6
  • 64
  • 96
8

You should check out nl2br, which converts the newlines (which won't be visible on a HTML document - as you noticed) to the HTML-tag

Zar
  • 6,786
  • 8
  • 54
  • 76
  • Wow - it's that easy? How I not find this function earlier? Thanks man, that's awesome. :) – Chrispy Feb 19 '12 at 00:31
  • Vastly more superior and WYSIWYG-like: http://stackoverflow.com/a/7409591/2266583 treats single line breaks as line breaks, and double line breaks as new paragraphs – zanderwar Sep 29 '16 at 02:37
0

Nice simple valid HTML:

$html = '<p>'.implode('</p><p>', preg_split('/\R+/', $text)).'</p>';
  • 1
    Related (but closed): https://stackoverflow.com/q/5961217/2943403 Wait, I found the page that you should post on instead: https://stackoverflow.com/q/1041136/2943403 or https://stackoverflow.com/q/14905305/2943403 or https://stackoverflow.com/q/22183937/2943403 – mickmackusa Sep 22 '21 at 01:42
-1

In html code you can use. <br/> Read this function http://php.net/manual/en/function.nl2br.php It will help;)

czupe
  • 4,740
  • 7
  • 34
  • 52
  • There was 3 answer in the same time, all answer directed to the same function, you -1-ed two of them, because you think your answer is better:) Thanks Sheriff. – czupe Sep 30 '16 at 08:49
  • No problem Mate, I searched one of your similar reply in order to point it out that there are some cases when the right direction is almost as good or a better way to help than writing numerous lines and examples. This is obviously only my opinion:) Have a nice day! – czupe Oct 05 '16 at 15:08