Home » Php » Read in text file line by line php – newline not being detected

Read in text file line by line php – newline not being detected

Posted by: admin April 23, 2020 Leave a comment

Questions:

I have a php function I wrote that will take a text file and list each line as its own row in a table.

The problem is the classic “works fine on my machine”, but of course when I ask somebody else to generate the .txt file I am looking for, it keeps on reading in the whole file as 1 line. When I open it in my text editor, it looks just as how I would expect it with a new name on each line, but its the newline character or something throwing it off.

So far I have come to the conclusion it might have something to do with whatever text editor they are using on their Mac system.

Does this make sense? and is there any easy way to just detect this character that the text editor is recognizing as a new line and replace it with a standard one that php will recognize?

UPDATE: Adding the following line solved the issue.

ini_set('auto_detect_line_endings',true);

Function:

function displayTXTList($fileName) {
    if(file_exists($fileName)) {
        $file = fopen($fileName,'r');
        while(!feof($file)) { 
            $name = fgets($file);
            echo('<tr><td align="center">'.$name.'</td></tr>');
        }
        fclose($file);
    } else {
        echo('<tr><td align="center">placeholder</td></tr>');
    }       
}
How to&Answers:

This doesn’t work for you?

http://us2.php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-endings

Answer:

What’s wrong with file()?

foreach (file($fileName) as $name) {
    echo('<tr><td align="center">'.$name.'</td></tr>');
}

Answer:

From the man page of fgets:

Note: If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.

Also, have you tried the file function? It returns an array; each element in the array corresponds to a line in the file.

Edit: if you don’t have access to the php.ini, what web server are you using? In Apache, you can change PHP settings using a .htaccess file. There is also the ini_set function which allows changing settings at runtime.

Answer:

This is a classic case of the newline problem.

ASCII defines several different “newline” characters. The two specific ones we care about are ASCII 10 (line feed, LF) and 13 (carriage return, CR).

All Unix-based systems, including OS X, Linux, etc. will use LF as a newline. Mac OS Classic used CR just to be different, and Windows uses CR LF (that’s right, two characters for a newline – see why no one likes Windows? Just kidding) as a newline.

Hence, text files from someone on a Mac (assuming it’s a modern OS) would all have LF as their line ending. If you’re trying to read them on Windows, and Windows expects CR LF, it won’t find it. Now, it has already been mentioned that PHP has the ability to sort this mess out for you, but if you prefer, here’s a memory-hogging solution:

$file = file_get_contents("filename");
$array = split("/25?/", $file); # won't work for Mac Classic

Of course, you can do the same thing with file() (as has already been mentioned).