Read in text file line by line php - newline not being detected
2012-05-14 22:59
453 查看
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>');
}
}
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>');
}
}
相关文章推荐
- read file by bash shell just as getline() in c++
- php - Fatal error: Unable to read X bytes in file.php on line 0
- The page you requested, favicon.ico, could not be found. in file **/system/core/Kohana.php on line 841
- The idcode read from the device does not match the idcode in the bsdl File.
- /dev/sdc1 is apparently in use by the system; will not make a filesystem here!
- &lt;?php}?&gt;格式导致错误 Parse error: syntax error, unexpected end of file in *.php on line 374
- Operation category READ is not supported in state standby
- /dev/sdxx is apparently in use by the system; will not make a filesystem here! 解决方法
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- 报错Archive for required library: 'XXX' in project 'XXX' cannot be read or is not a valid ZIP file
- PHP显示Deprecated: Assigning the return value of new by reference is deprecated in解决办法
- Golang Read File line by line
- /libexpat.so: could not read symbols: File in wrong format
- Archive for required library: "xxxx" in project '*' cannot be read or is not a valid ZIP file
- Deprecated: Call-time pass-by-reference has been deprecated in E:\wamp\www\admin\htdocs\busi.php on line 381
- PHPExcel_Reader_Exception: is not recognised as an OLE file in Classes
- php5.3 中显示Deprecated: Assigning the return value of new by reference is deprecated in 的解决方法
- Laravel No such file or directory in /bootstrap/autoload.php on line 17
- The method makeText(Context, int, int) in the type Toast is not applicable for the arguments (new Vi
- unexpected end of file in HotNewspro\includes\directory.php on line 22