Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ PHP NEWS
. Fixed bug GH-13204 (glob() fails if square bracket is in current directory).
(ndossche)
. Add array size maximum to array_diff(). (ndossche)
. Fixed bug GH-18120 (Honor FILE_SKIP_EMPTY_LINES even when
FILE_IGNORE_NEW_LINES is not set). (alexandre-daubois)

- Streams:
. Added so_keepalive, tcp_keepidle, tcp_keepintvl and tcp_keepcnt stream
Expand Down
10 changes: 10 additions & 0 deletions ext/standard/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,16 @@ PHP_FUNCTION(file)
do {
p++;
parse_eol:
if (skip_blank_lines) {
const char *c = s;
while (c < p && (*c == '\n' || *c == '\r')) {
c++;
}
if (c == p) {
s = p;
continue;
}
}
add_index_stringl(return_value, i++, s, p-s);
s = p;
} while ((p = memchr(p, eol_marker, (e-p))));
Expand Down
131 changes: 131 additions & 0 deletions ext/standard/tests/file/file_skip_empty_lines.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
--TEST--
GH-18120 (Honor FILE_SKIP_EMPTY_LINES even when FILE_IGNORE_NEW_LINES is not set)
--FILE--
<?php
$test_file = __DIR__ . "/file_skip_empty_lines.tmp";

$test_data = "First\n\nSecond\n\n\nThird\n";
file_put_contents($test_file, $test_data);

$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

$lines = file($test_file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
var_dump($lines);

$lines = file($test_file, FILE_IGNORE_NEW_LINES);
var_dump($lines);

$lines = file($test_file);
var_dump($lines);

$test_data_win = "First\r\n\r\nSecond\r\n";
file_put_contents($test_file, $test_data_win);

$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

$lines = file($test_file, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES);
var_dump($lines);

file_put_contents($test_file, "\r");
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

file_put_contents($test_file, "\r\r");
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

file_put_contents($test_file, "\n");
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

file_put_contents($test_file, "\r\n");
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

file_put_contents($test_file, "\r\r\n\n");
$lines = file($test_file, FILE_SKIP_EMPTY_LINES);
var_dump($lines);

?>
--CLEAN--
<?php unlink(__DIR__ . "/file_skip_empty_lines.tmp"); ?>
--EXPECT--
array(3) {
[0]=>
string(6) "First
"
[1]=>
string(7) "Second
"
[2]=>
string(6) "Third
"
}
array(3) {
[0]=>
string(5) "First"
[1]=>
string(6) "Second"
[2]=>
string(5) "Third"
}
array(6) {
[0]=>
string(5) "First"
[1]=>
string(0) ""
[2]=>
string(6) "Second"
[3]=>
string(0) ""
[4]=>
string(0) ""
[5]=>
string(5) "Third"
}
array(6) {
[0]=>
string(6) "First
"
[1]=>
string(1) "
"
[2]=>
string(7) "Second
"
[3]=>
string(1) "
"
[4]=>
string(1) "
"
[5]=>
string(6) "Third
"
}
array(2) {
[0]=>
string(7) "First
"
[1]=>
string(8) "Second
"
}
array(2) {
[0]=>
string(5) "First"
[1]=>
string(6) "Second"
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
array(0) {
}
10 changes: 2 additions & 8 deletions ext/standard/tests/file/file_variation.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,14 @@ array(5) {
[4]=>
string(5) " data"
}
array(5) {
array(3) {
[0]=>
string(4) "Gar
"
[1]=>
string(1) "
"
[2]=>
string(6) "bage
"
[3]=>
string(1) "
"
[4]=>
[2]=>
string(5) " data"
}
*** Testing with variation in use_include_path argument ***
Expand Down
9 changes: 3 additions & 6 deletions ext/standard/tests/file/file_variation7.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,17 @@ array(5) {
}

file() with FILE_SKIP_EMPTY_LINES:
array(5) {
array(4) {
[0]=>
string(7) "Line 1
"
[1]=>
string(1) "
"
[2]=>
string(2) "
"
[3]=>
[2]=>
string(3) "
"
[4]=>
[3]=>
string(7) "\Line 3"
}

Expand Down
Loading