php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44034 FILE_IGNORE_NEW_LINES in FILE does not work as expected when lines end in \r\n
Submitted: 2008-02-04 08:40 UTC Modified: 2010-06-21 00:29 UTC
Votes:8
Avg. Score:3.9 ± 0.8
Reproduced:8 of 8 (100.0%)
Same Version:1 (12.5%)
Same OS:3 (37.5%)
From: php at commerco dot net Assigned:
Status: Wont fix Package: Filesystem function related
PHP Version: 6CVS-2008-02-04 (snap) OS: Win2K
Private report: No CVE-ID: None
 [2008-02-04 08:40 UTC] php at commerco dot net
Description:
------------
Using FILE, when the parameter FILE_IGNORE_NEW_LINES is used, if the file uses a line termination of \n the operation works as expected.

If the file uses a line termination of \r\n (MS Windows), the \r is not removed.

One might argue that the NEW_LINES is \n alone, and of course FILE works fine when the file has a \n terminator, however, \r\n is a common line record (line) terminator on a windows system.  If memory serves, this kind of issue has been addressed in some other PHP functions, so it would be great if you would consider addressing this one too.

The problem really becomes apparent if you take two arrays derived from FILE functions and combine them via an array_combine.

Reproduce code:
---------------
/* create 2 files in notepad.
$somefile1 data:
foo
bar
etc
$somefile2 data:
123
456
789
*/
$a1 = file($somefile1, FILE_IGNORE_NEW_LINES+FILE_TEXT);
$a2 = file($somefile2, FILE_IGNORE_NEW_LINES+FILE_TEXT);
$a3 = array_combine($a1, $a2);
print_r($a3);

Expected result:
----------------
$a3 is an array with three entries with both key and data each entry having a length of 3 bytes.

Array
(
    [foo] => 123
    [bar] => 456
    [etc] => 789
)

(which is what happens when a file contains \n terminated lines).


Actual result:
--------------
$a3 is an array with three entries with both key and data each entry having a length of 4 bytes.

Array
(
    [foo
] => 123
    [bar
] => 456
    [etc
] => 789
)

(which is what happens when a file contains \r\n terminated lines).


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-02-10 06:57 UTC] php at commerco dot net
Thank you for your post.  Unfortunately, the current build version (php6.0-win32-200802041530) did not cure the issue.
 [2010-06-21 00:29 UTC] felipe@php.net
-Status: Open +Status: Wont fix
 [2010-06-21 00:29 UTC] felipe@php.net
Old trunk related.
 [2021-04-20 12:13 UTC] php at yopmail dot com
Fixed in PHP 5.2.10 / 5.3.0 and higher <https://3v4l.org/eA9gM>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 12 17:01:31 2024 UTC