php 逐行读取文本文件php-未检测到换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/817783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read in text file line by line php - newline not being detected
提问by TaRDy
I have a php function I wrote that will take a text file and list each line as its own row in a table.
我有一个我编写的 php 函数,它将采用一个文本文件并将每一行列为表中的一行。
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.
问题是经典的“在我的机器上工作正常”,但是当然,当我要求其他人生成我正在寻找的 .txt 文件时,它会继续读取整个文件作为 1 行。当我在文本编辑器中打开它时,它看起来就像我期望的那样,每一行都有一个新名称,但它是换行符或其他东西。
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.
到目前为止,我得出的结论可能与他们在 Mac 系统上使用的任何文本编辑器有关。
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?
这有意义吗?是否有任何简单的方法可以检测文本编辑器识别为新行的字符并将其替换为 php 可以识别的标准字符?
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>');
}
}
采纳答案by alphazero
回答by jmucchiello
回答by Ayman Hourieh
From the man page of fgets:
从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_endingsrun-time configuration option may help resolve the problem.
注意:如果 PHP 在读取 Macintosh 计算机上的文件或由 Macintosh 计算机创建的文件时无法正确识别行尾,启用auto_detect_line_endings运行时配置选项可能有助于解决问题。
Also, have you tried the filefunction? 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_setfunction which allows changing settings at runtime.
编辑:如果您无权访问 php.ini,您使用的是什么 Web 服务器?在 Apache 中,您可以使用.htaccess 文件更改 PHP 设置。还有ini_set函数允许在运行时更改设置。
回答by Chris Lutz
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).
ASCII 定义了几个不同的“换行符”字符。我们关心的两个特定字符是 ASCII 10(换行,LF)和 13(回车,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.
所有基于 Unix 的系统,包括 OS X、Linux 等,都将使用 LF 作为换行符。Mac OS Classic 使用 CR 只是为了与众不同,而 Windows 使用 CR LF(没错,换行符是两个字符 - 看看为什么没有人喜欢 Windows?开个玩笑)作为换行符。
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 mentionedthat PHP has the ability to sort this mess out for you, but if you prefer, here's a memory-hogging solution:
因此,来自 Mac 上某人的文本文件(假设它是现代操作系统)都将 LF 作为行尾。如果您尝试在 Windows 上阅读它们,而 Windows 需要 CR LF,则它不会找到它。现在,已经提到PHP 有能力为您解决这个问题,但如果您愿意,这里有一个占用内存的解决方案:
$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).
当然,你可以用file()(已经提到过)做同样的事情。

