php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41315 file() should support FILE_SKIP_EMPTY_LINES without FILE_IGNORE_NEW_LINES
Submitted: 2007-05-07 16:19 UTC Modified: 2016-06-22 18:00 UTC
Votes:24
Avg. Score:4.5 ± 1.1
Reproduced:23 of 24 (95.8%)
Same Version:10 (43.5%)
Same OS:10 (43.5%)
From: cschneid at cschneid dot com Assigned:
Status: Open Package: *General Issues
PHP Version: 5.2.2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cschneid at cschneid dot com
New email:
PHP Version: OS:

 

 [2007-05-07 16:19 UTC] cschneid at cschneid dot com
Description:
------------
Currently file() only skips (FILE_SKIP_EMPTY_LINES) when also using      
FILE_IGNORE_NEW_LINES at the same time. I think it would be      
preferable if it would work without that flag too, ignoring lines      
only consisting of a newline.     
 
Patch  
diff -u -r1.409.2.6.2.17 file.c 
--- ext/standard/file.c 23 Feb 2007 16:22:20 -0000      
1.409.2.6.2.17 
+++ ext/standard/file.c 7 May 2007 16:17:27 -0000 
@@ -750,6 +750,10 @@ 
                        do { 
                                p++; 
 parse_eol: 
+                               if (skip_blank_lines && (p-s == 1) 
&& (*s == eol_marker)) { 
+                                       s = p; 
+                                       continue; 
+                               } 
                                if (PG(magic_quotes_runtime)) { 
                                        /* s is in target_buf which 
is freed at the end of the function */ 
                                        slashed = php_addslashes(s, 
(p-s), &len, 0 TSRMLS_CC); 
 

Reproduce code:
---------------
Input file x:
a   
   
b

Code:
var_dump(file("x", FILE_SKIP_EMPTY_LINES));

Expected result:
----------------
a 
b 
 

Actual result:
--------------
a 
 
b 
 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-05-10 19:43 UTC] theanomaly dot is at gmail dot com
The reason FILE_SKIP_EMPTY_LINES doesn't skip any lines without using it in 
combination with FILE_IGNORE_NEW_LINES is because to PHP the lines still aren't 
empty until they amount to a 0 length string. With the EOL character (or 
characters in the case of CRLF) still a part of the string the lines will remain 
a part of the returned array from file().

However, since this technically doesn't break any BC (given that the 
FILE_SKIP_EMPTY_LINES flag doesn't do anything without FILE_IGNORE_NEW_LINES) I 
don't see why it would be a bad idea to support this when the only thing 
remaining on the line is the EOL.

I've offered to submit this as a feature request instead and will also submit a 
patch for the documentation to clarify this behavior.

https://github.com/php/php-src/pull/80
 [2016-06-22 18:00 UTC] cmb@php.net
-Package: Feature/Change Request +Package: *General Issues
 [2016-06-22 18:00 UTC] cmb@php.net
JFTR: the PR had been closed in the meantime without having been
merged.
 [2024-07-11 11:35 UTC] chris1058wright at outlook dot com
I was actually reading your article and found some really interesting information. The thing is quite clear that I just want to thank for it. (https://github.com)(https://www.ny-stateofhealth.com)
 [2024-07-13 07:27 UTC] Victoria476Huff at outlook dot com
Great post. It was much needed. Love your simplistic style of explanation. (https://github.com)(https://www-myloyola.com)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC