diff --git a/UPGRADING b/UPGRADING index 723d6b28f7a3e..cc6dabc6644f8 100644 --- a/UPGRADING +++ b/UPGRADING @@ -429,6 +429,11 @@ PHP 8.5 UPGRADE NOTES . socket_set_option with multicast context throws a ValueError when the created socket is not of AF_INET/AF_INET6 family. +- Standard: + . file() now correctly honors the FILE_SKIP_EMPTY_LINES flag even when + FILE_IGNORE_NEW_LINES is not set. Previously, FILE_SKIP_EMPTY_LINES + only worked when combined with FILE_IGNORE_NEW_LINES. See GH-18120. + - Tidy: . tidy::__construct/parseFile/parseString now throws a ValueError if the configuration contains an invalid or set a read-only diff --git a/ext/standard/file.c b/ext/standard/file.c index ab6ed4fbadd2d..a72fa8953a881 100644 --- a/ext/standard/file.c +++ b/ext/standard/file.c @@ -640,6 +640,16 @@ PHP_FUNCTION(file) do { p++; parse_eol: + if (skip_blank_lines) { + int windows_eol = 0; + if (p != ZSTR_VAL(target_buf) && eol_marker == '\n' && *(p - 1) == '\r') { + windows_eol++; + } + if (p-s-windows_eol == 1) { + s = p; + continue; + } + } add_index_stringl(return_value, i++, s, p-s); s = p; } while ((p = memchr(p, eol_marker, (e-p)))); diff --git a/ext/standard/tests/file/file_skip_empty_lines.phpt b/ext/standard/tests/file/file_skip_empty_lines.phpt new file mode 100644 index 0000000000000..88093aae81615 --- /dev/null +++ b/ext/standard/tests/file/file_skip_empty_lines.phpt @@ -0,0 +1,103 @@ +--TEST-- +Test file() function with FILE_SKIP_EMPTY_LINES flag +--FILE-- + +--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(3) { + [0]=> + string(7) "First +" + [1]=> + string(2) " +" + [2]=> + string(8) "Second +" +} +array(2) { + [0]=> + string(5) "First" + [1]=> + string(6) "Second" +} diff --git a/ext/standard/tests/file/file_variation.phpt b/ext/standard/tests/file/file_variation.phpt index 2c46f039c2398..3a69c0bc24eb1 100644 --- a/ext/standard/tests/file/file_variation.phpt +++ b/ext/standard/tests/file/file_variation.phpt @@ -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 *** diff --git a/ext/standard/tests/file/file_variation7.phpt b/ext/standard/tests/file/file_variation7.phpt index 72af244a54e17..15d1f319a91d3 100644 --- a/ext/standard/tests/file/file_variation7.phpt +++ b/ext/standard/tests/file/file_variation7.phpt @@ -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" }