import text is all on one line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jiranz
Forum Newbie
Posts: 3
Joined: Sat Mar 05, 2011 5:06 pm

import text is all on one line

Post by jiranz »

http://www.jiranz.com/test.txt
http://www.jiranz.com/test.php

The text file displays correctly in my browser but the same file displays as a single line of text when imported with php.
I have experimented with php scripts to import lists and even tried csv but the returned text always only a single line.
I seems that the php script must remove the line feeds?
How do I get the php script to display the text file correctly?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: import text is all on one line

Post by Christopher »

No, the browser is displaying the text file as a text file -- using the newlines. But the PHP script is rendered as a HTML document where any whitespace is rendered as a space. You could do something like:

Code: Select all

echo str_replace("\n", '<br/>', $file_contents);
(#10850)
jiranz
Forum Newbie
Posts: 3
Joined: Sat Mar 05, 2011 5:06 pm

Re: import text is all on one line

Post by jiranz »

Christopher wrote:No, the browser is displaying the text file as a text file -- using the newlines. But the PHP script is rendered as a HTML document where any whitespace is rendered as a space. You could do something like:

Code: Select all

echo str_replace("\n", '<br/>', $file_contents);
This is what is in my php file at present:

Code: Select all

<?php include("test.txt") ?>
So you are suggesting I use an alternative php script to read line by line and add in the break code?

For example where do i add it to this line by line script borrowed from the manual (which otherwise returns the same results as the above script)

Code: Select all

<?php
$handle = @fopen("test.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>
Thanks for your input :)
jiranz
Forum Newbie
Posts: 3
Joined: Sat Mar 05, 2011 5:06 pm

Re: import text is all on one line

Post by jiranz »

I got a working script...

Code: Select all

<?php
// get contents of a file into a string
$filename = "test.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
$fp = fopen('data:text/plain,'. $contents,'rb');
while ( ($line = fgets($fp)) !== false) {
  echo "$line<br>";
}
?>
Your explanation helped and I can learn from this.
Thanks.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: import text is all on one line

Post by Christopher »

Well, I was assuming that your were reading the whole file.

Code: Select all

$filename = "test.txt";
$file_contents = file_get_contents($filename);
echo str_replace("\n", '<br/>', $file_contents);     // this also might be "\r"
(#10850)
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: import text is all on one line

Post by litebearer »

Or...

Code: Select all

$filename = "test.txt";
$file_contents = file_get_contents($filename);
echo nl2br($file_contents);  
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: import text is all on one line

Post by Christopher »

Even better!
(#10850)
Post Reply