逐行读取文本文件php-未检测到换行符 [英] Read in text file line by line php - newline not being detected
问题描述
我有一个编写的php函数,它将获取一个文本文件,并将每一行作为表格中的单独行列出.
I have a php function I wrote that will take a text file and list each line as its own row in a table.
问题是经典的在我的机器上可以正常工作",但是当然,当我要求别人生成我要查找的.txt文件时,它会以1行的形式继续读取整个文件.当我在文本编辑器中打开它时,它看起来就像我期望的那样,在每行上都有一个新名称,但是它是换行符或其他东西.
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.
到目前为止,我得出的结论可能与他们在Mac系统上使用的任何文本编辑器有关.
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.
这有意义吗?并且有什么简单的方法来检测文本编辑器将其识别为换行符并将其替换为php可以识别的标准字符吗?
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 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>');
}
}
推荐答案
这对您不起作用?
http://us2. php.net/manual/en/filesystem.configuration.php#ini.auto-detect-line-ends
这篇关于逐行读取文本文件php-未检测到换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!