Make CSV deprecation less annoying to deal with (#15569)

This commit is contained in:
Gina Peter Banyard 2024-09-13 15:07:26 +01:00 committed by GitHub
parent e73a855976
commit f756b96e06
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
88 changed files with 411 additions and 344 deletions

View file

@ -581,11 +581,11 @@ PHP 8.4 UPGRADE NOTES
- SPL: - SPL:
. The SplFixedArray::__wakeup() method has been deprecated as it implements . The SplFixedArray::__wakeup() method has been deprecated as it implements
__serialize() and __unserialize() which need to be overwritten instead. __serialize() and __unserialize() which need to be overwritten instead.
. Passing a non-empty string for the $enclosure parameter of: . Using the default value for $escape parameter of:
- SplFileObject::setCsvControl() - SplFileObject::setCsvControl()
- SplFileObject::fputcsv() - SplFileObject::fputcsv()
- SplFileObject::fgetcsv() - SplFileObject::fgetcsv()
is now deprecated. is now deprecated. It must be passed explicitly either positionally or via named arguments.
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_proprietary_csv_escaping_mechanism RFC: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_proprietary_csv_escaping_mechanism
- Standard: - Standard:
@ -595,11 +595,11 @@ PHP 8.4 UPGRADE NOTES
RFC: https://wiki.php.net/rfc/raising_zero_to_power_of_negative_number RFC: https://wiki.php.net/rfc/raising_zero_to_power_of_negative_number
. Unserializing strings using the uppercase 'S' tag is deprecated. . Unserializing strings using the uppercase 'S' tag is deprecated.
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#unserialize_s_s_tag RFC: https://wiki.php.net/rfc/deprecations_php_8_4#unserialize_s_s_tag
. Passing a non-empty string for the $enclosure parameter of: . Using the default value for $escape parameter of:
- fputcsv() - fputcsv()
- fgetcsv() - fgetcsv()
- str_getcsv() - str_getcsv()
is now deprecated. is now deprecated. It must be passed explicitly either positionally or via named arguments.
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_proprietary_csv_escaping_mechanism RFC: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_proprietary_csv_escaping_mechanism
- XML: - XML:

View file

@ -33,7 +33,7 @@ class MyCSVFile extends SplFileObject
{ {
function current(): array|false function current(): array|false
{ {
return parent::fgetcsv(',', '"'); return parent::fgetcsv(',', '"', escape: '');
} }
} }
@ -44,14 +44,14 @@ $v = $phar['a.csv'];
echo "===3===\n"; echo "===3===\n";
while(!$v->eof()) while(!$v->eof())
{ {
echo $v->key() . "=>" . join('|', $v->fgetcsv()) . "\n"; echo $v->key() . "=>" . join('|', $v->fgetcsv(escape: '')) . "\n";
} }
echo "===4===\n"; echo "===4===\n";
$v->rewind(); $v->rewind();
while(!$v->eof()) while(!$v->eof())
{ {
$l = $v->fgetcsv(); $l = $v->fgetcsv(escape: '');
echo $v->key() . "=>" . join('|', $l) . "\n"; echo $v->key() . "=>" . join('|', $l) . "\n";
} }
@ -66,7 +66,7 @@ class MyCSVFile2 extends SplFileObject
function getCurrentLine(): string function getCurrentLine(): string
{ {
echo __METHOD__ . "\n"; echo __METHOD__ . "\n";
return implode('|', parent::fgetcsv(',', '"')); return implode('|', parent::fgetcsv(',', '"', escape: ''));
} }
} }

View file

@ -365,6 +365,7 @@ static zend_result spl_filesystem_file_open(spl_filesystem_object *intern, bool
intern->u.file.delimiter = ','; intern->u.file.delimiter = ',';
intern->u.file.enclosure = '"'; intern->u.file.enclosure = '"';
intern->u.file.escape = (unsigned char) '\\'; intern->u.file.escape = (unsigned char) '\\';
intern->u.file.is_escape_default = true;
intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1); intern->u.file.func_getCurr = zend_hash_str_find_ptr(&intern->std.ce->function_table, "getcurrentline", sizeof("getcurrentline") - 1);
@ -2273,16 +2274,33 @@ PHP_METHOD(SplFileObject, getChildren)
/* return NULL */ /* return NULL */
} /* }}} */ } /* }}} */
static int spl_csv_enclosure_param_handling(const zend_string* escape_str, const spl_filesystem_object *intern, uint32_t arg_num)
{
if (escape_str == NULL) {
if (intern->u.file.is_escape_default) {
php_error_docref(NULL, E_DEPRECATED, "the $escape parameter must be provided,"
" as its default value will change,"
" either explicitly or via SplFileObject::setCsvControl()");
if (UNEXPECTED(EG(exception))) {
return PHP_CSV_ESCAPE_ERROR;
}
}
return intern->u.file.escape;
} else {
return php_csv_handle_escape_argument(escape_str, arg_num);
}
}
/* {{{ Return current line as CSV */ /* {{{ Return current line as CSV */
PHP_METHOD(SplFileObject, fgetcsv) PHP_METHOD(SplFileObject, fgetcsv)
{ {
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)); spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure; char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure;
int escape = intern->u.file.escape; char *delim = NULL, *enclo = NULL;
char *delim = NULL, *enclo = NULL, *esc = NULL; size_t d_len = 0, e_len = 0;
size_t d_len = 0, e_len = 0, esc_len = 0; zend_string *escape_str = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ssS", &delim, &d_len, &enclo, &e_len, &escape_str) == FAILURE) {
RETURN_THROWS(); RETURN_THROWS();
} }
@ -2302,23 +2320,12 @@ PHP_METHOD(SplFileObject, fgetcsv)
} }
enclosure = enclo[0]; enclosure = enclo[0];
} }
if (esc) { int escape_char = spl_csv_enclosure_param_handling(escape_str, intern, 3);
if (esc_len > 1) { if (escape_char == PHP_CSV_ESCAPE_ERROR) {
zend_argument_value_error(3, "must be empty or a single character");
RETURN_THROWS(); RETURN_THROWS();
} }
if (esc_len == 0) {
escape = PHP_CSV_NO_ESCAPE;
} else {
php_error_docref(NULL, E_DEPRECATED, "Passing a non-empty string to the $escape parameter is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
escape = (unsigned char) esc[0];
}
}
if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape, return_value, true) == FAILURE) { if (spl_filesystem_file_read_csv(intern, delimiter, enclosure, escape_char, return_value, true) == FAILURE) {
RETURN_FALSE; RETURN_FALSE;
} }
} }
@ -2329,14 +2336,14 @@ PHP_METHOD(SplFileObject, fputcsv)
{ {
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)); spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure; char delimiter = intern->u.file.delimiter, enclosure = intern->u.file.enclosure;
int escape = intern->u.file.escape; char *delim = NULL, *enclo = NULL;
char *delim = NULL, *enclo = NULL, *esc = NULL; size_t d_len = 0, e_len = 0;
size_t d_len = 0, e_len = 0, esc_len = 0;
zend_long ret; zend_long ret;
zval *fields = NULL; zval *fields = NULL;
zend_string *escape_str = NULL;
zend_string *eol = NULL; zend_string *eol = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|sssS", &fields, &delim, &d_len, &enclo, &e_len, &esc, &esc_len, &eol) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "a|ssSS", &fields, &delim, &d_len, &enclo, &e_len, &escape_str, &eol) == FAILURE) {
RETURN_THROWS(); RETURN_THROWS();
} }
@ -2354,23 +2361,12 @@ PHP_METHOD(SplFileObject, fputcsv)
} }
enclosure = enclo[0]; enclosure = enclo[0];
} }
if (esc) { int escape_char = spl_csv_enclosure_param_handling(escape_str, intern, 4);
if (esc_len > 1) { if (escape_char == PHP_CSV_ESCAPE_ERROR) {
zend_argument_value_error(4, "must be empty or a single character");
RETURN_THROWS(); RETURN_THROWS();
} }
if (esc_len == 0) {
escape = PHP_CSV_NO_ESCAPE;
} else {
php_error_docref(NULL, E_DEPRECATED, "Passing a non-empty string to the $escape parameter is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
escape = (unsigned char) esc[0];
}
}
ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape, eol); ret = php_fputcsv(intern->u.file.stream, fields, delimiter, enclosure, escape_char, eol);
if (ret < 0) { if (ret < 0) {
RETURN_FALSE; RETURN_FALSE;
} }
@ -2383,11 +2379,11 @@ PHP_METHOD(SplFileObject, setCsvControl)
{ {
spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS)); spl_filesystem_object *intern = spl_filesystem_from_obj(Z_OBJ_P(ZEND_THIS));
char delimiter = ',', enclosure = '"'; char delimiter = ',', enclosure = '"';
int escape = (unsigned char) '\\'; char *delim = NULL, *enclo = NULL;
char *delim = NULL, *enclo = NULL, *esc = NULL; size_t d_len = 0, e_len = 0;
size_t d_len = 0, e_len = 0, esc_len = 0; zend_string *escape_str = NULL;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "|sss", &delim, &d_len, &enclo, &e_len, &esc, &esc_len) == FAILURE) { if (zend_parse_parameters(ZEND_NUM_ARGS(), "|ssS", &delim, &d_len, &enclo, &e_len, &escape_str) == FAILURE) {
RETURN_THROWS(); RETURN_THROWS();
} }
@ -2405,25 +2401,17 @@ PHP_METHOD(SplFileObject, setCsvControl)
} }
enclosure = enclo[0]; enclosure = enclo[0];
} }
if (esc) { int escape_char = php_csv_handle_escape_argument(escape_str, 3);
if (esc_len > 1) { if (escape_char == PHP_CSV_ESCAPE_ERROR) {
zend_argument_value_error(3, "must be empty or a single character");
RETURN_THROWS(); RETURN_THROWS();
} }
if (esc_len == 0) { if (escape_str != NULL) {
escape = PHP_CSV_NO_ESCAPE; intern->u.file.is_escape_default = false;
} else {
php_error_docref(NULL, E_DEPRECATED, "Passing a non-empty string to the $escape parameter is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
escape = (unsigned char) esc[0];
}
} }
intern->u.file.delimiter = delimiter; intern->u.file.delimiter = delimiter;
intern->u.file.enclosure = enclosure; intern->u.file.enclosure = enclosure;
intern->u.file.escape = escape; intern->u.file.escape = escape_char;
} }
/* }}} */ /* }}} */

View file

@ -82,6 +82,7 @@ struct _spl_filesystem_object {
char delimiter; char delimiter;
char enclosure; char enclosure;
int escape; int escape;
bool is_escape_default;
} file; } file;
} u; } u;
zend_object std; zend_object std;

View file

@ -3,15 +3,20 @@ SplFileObject::fgetcsv default path
--FILE-- --FILE--
<?php <?php
$fp = fopen('SplFileObject__fgetcsv1.csv', 'w+'); $fp = fopen('SplFileObject__fgetcsv1.csv', 'w+');
fputcsv($fp, array( fputcsv(
$fp,
[
'field1', 'field1',
'field2', 'field2',
'field3', 'field3',
5 5,
)); ],
escape: '',
);
fclose($fp); fclose($fp);
$fo = new SplFileObject('SplFileObject__fgetcsv1.csv'); $fo = new SplFileObject('SplFileObject__fgetcsv1.csv');
$fo->setCsvControl(escape: '');
var_dump($fo->fgetcsv()); var_dump($fo->fgetcsv());
?> ?>
--CLEAN-- --CLEAN--

View file

@ -1,17 +1,23 @@
--TEST-- --TEST--
SplFileObject::fgetcsv with alternative delimiter SplFileObject::fgetcsv with alternative separator
--FILE-- --FILE--
<?php <?php
$fp = fopen('SplFileObject__fgetcsv2.csv', 'w+'); $fp = fopen('SplFileObject__fgetcsv2.csv', 'w+');
fputcsv($fp, array( fputcsv(
$fp,
[
'field1', 'field1',
'field2', 'field2',
'field3', 'field3',
5 5,
), '|'); ],
separator: '|',
escape: '',
);
fclose($fp); fclose($fp);
$fo = new SplFileObject('SplFileObject__fgetcsv2.csv'); $fo = new SplFileObject('SplFileObject__fgetcsv2.csv');
$fo->setCsvControl(escape: '');
var_dump($fo->fgetcsv('|')); var_dump($fo->fgetcsv('|'));
?> ?>
--CLEAN-- --CLEAN--

View file

@ -1,17 +1,22 @@
--TEST-- --TEST--
SplFileObject::fgetcsv with alternative delimiter SplFileObject::fgetcsv() delimiter error
--FILE-- --FILE--
<?php <?php
$fp = fopen('SplFileObject__fgetcsv3.csv', 'w+'); $fp = fopen('SplFileObject__fgetcsv3.csv', 'w+');
fputcsv($fp, array( fputcsv(
$fp,
[
'field1', 'field1',
'field2', 'field2',
'field3', 'field3',
5 5,
), '|'); ],
escape: '',
);
fclose($fp); fclose($fp);
$fo = new SplFileObject('SplFileObject__fgetcsv3.csv'); $fo = new SplFileObject('SplFileObject__fgetcsv3.csv');
$fo->setCsvControl(escape: '');
try { try {
var_dump($fo->fgetcsv('invalid')); var_dump($fo->fgetcsv('invalid'));
} catch (ValueError $e) { } catch (ValueError $e) {

View file

@ -1,18 +1,24 @@
--TEST-- --TEST--
SplFileObject::fgetcsv with alternative delimiter SplFileObject::fgetcsv with alternative enclosure
--FILE-- --FILE--
<?php <?php
$fp = fopen('SplFileObject__fgetcsv4.csv', 'w+'); $fp = fopen('SplFileObject__fgetcsv4.csv', 'w+');
fputcsv($fp, array( fputcsv(
$fp,
[
'field1', 'field1',
'field2', 'field2',
'field3', 'field3',
5 5,
), ',', '"'); ],
enclosure: '"',
escape: '',
);
fclose($fp); fclose($fp);
$fo = new SplFileObject('SplFileObject__fgetcsv4.csv'); $fo = new SplFileObject('SplFileObject__fgetcsv4.csv');
var_dump($fo->fgetcsv(',', '"')); $fo->setCsvControl(escape: '');
var_dump($fo->fgetcsv(enclosure: '"'));
?> ?>
--CLEAN-- --CLEAN--
<?php <?php

View file

@ -1,19 +1,24 @@
--TEST-- --TEST--
SplFileObject::fgetcsv with alternative delimiter SplFileObject::fgetcsv() enclosure error
--FILE-- --FILE--
<?php <?php
$fp = fopen('SplFileObject__fgetcsv5.csv', 'w+'); $fp = fopen('SplFileObject__fgetcsv5.csv', 'w+');
fputcsv($fp, array( fputcsv(
$fp,
[
'field1', 'field1',
'field2', 'field2',
'field3', 'field3',
5 5,
), ',', '"'); ],
escape: '',
);
fclose($fp); fclose($fp);
$fo = new SplFileObject('SplFileObject__fgetcsv5.csv'); $fo = new SplFileObject('SplFileObject__fgetcsv5.csv');
$fo->setCsvControl(escape: '');
try { try {
var_dump($fo->fgetcsv(',', 'invalid')); var_dump($fo->fgetcsv(enclosure: 'invalid'));
} catch (ValueError $e) { } catch (ValueError $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
} }

View file

@ -1,5 +1,5 @@
--TEST-- --TEST--
SplFileObject::fgetcsv with alternative delimiter SplFileObject::fgetcsv with alternative escape
--FILE-- --FILE--
<?php <?php
$fp = fopen('SplFileObject__fgetcsv6.csv', 'w+'); $fp = fopen('SplFileObject__fgetcsv6.csv', 'w+');
@ -14,7 +14,6 @@ var_dump($fo->fgetcsv(',', '"', '"'));
unlink('SplFileObject__fgetcsv6.csv'); unlink('SplFileObject__fgetcsv6.csv');
?> ?>
--EXPECTF-- --EXPECTF--
Deprecated: SplFileObject::fgetcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
array(3) { array(3) {
[0]=> [0]=>
string(3) "aaa" string(3) "aaa"

View file

@ -13,7 +13,8 @@ var_dump($fo->fgetcsv());
<?php <?php
unlink('SplFileObject__fgetcsv7.csv'); unlink('SplFileObject__fgetcsv7.csv');
?> ?>
--EXPECT-- --EXPECTF--
Deprecated: SplFileObject::fgetcsv(): the $escape parameter must be provided, as its default value will change, either explicitly or via SplFileObject::setCsvControl() in %s on line %d
array(3) { array(3) {
[0]=> [0]=>
string(4) "aa\"" string(4) "aa\""

View file

@ -1,5 +1,5 @@
--TEST-- --TEST--
SplFileObject::fgetcsv with alternative delimiter SplFileObject::fgetcsv() escape error
--FILE-- --FILE--
<?php <?php
$fp = fopen('SplFileObject__fgetcsv8.csv', 'w+'); $fp = fopen('SplFileObject__fgetcsv8.csv', 'w+');

View file

@ -4,6 +4,8 @@ SplFileObject::fputcsv(): functionality tests
<?php <?php
$file = __DIR__ . '/SplFileObject_fputcsv.csv'; $file = __DIR__ . '/SplFileObject_fputcsv.csv';
$fo = new SplFileObject($file, 'w'); $fo = new SplFileObject($file, 'w');
// Suppress deprecation notice
$fo->setCsvControl(escape: '\\');
$list = array ( $list = array (
0 => 'aaa,bbb', 0 => 'aaa,bbb',
@ -42,7 +44,7 @@ echo '$list = ';var_export($res);echo ";\n";
$fp = fopen($file, "r"); $fp = fopen($file, "r");
$res = array(); $res = array();
while($l=fgetcsv($fp)) while($l=fgetcsv($fp, escape: '\\'))
{ {
$res[] = join(',',$l); $res[] = join(',',$l);
} }

View file

@ -3,6 +3,7 @@ SplFileObject::fputcsv(): Checking data after calling the function
--FILE-- --FILE--
<?php <?php
$fo = new SplFileObject(__DIR__ . '/SplFileObject_fputcsv1.csv', 'w'); $fo = new SplFileObject(__DIR__ . '/SplFileObject_fputcsv1.csv', 'w');
$fo->setCsvControl(escape: '');
$data = array(1, 2, 'foo', 'haha', array(4, 5, 6), 1.3, null); $data = array(1, 2, 'foo', 'haha', array(4, 5, 6), 1.3, null);

View file

@ -42,6 +42,7 @@ foreach ($csv_lists as $csv_list) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '');
$delimiter = $csv_list[0]; $delimiter = $csv_list[0];
$enclosure = $csv_list[1]; $enclosure = $csv_list[1];
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];

View file

@ -36,6 +36,8 @@ foreach ($fields as $field) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '\\');
$csv_field = $field; $csv_field = $field;
// write to a file in csv format // write to a file in csv format

View file

@ -42,6 +42,8 @@ foreach ($csv_lists as $csv_list) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '\\');
$delimiter = $csv_list[0]; $delimiter = $csv_list[0];
$enclosure = $csv_list[1]; $enclosure = $csv_list[1];
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];

View file

@ -43,6 +43,8 @@ foreach ($csv_lists as $csv_list) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '\\');
$delimiter = $csv_list[0]; $delimiter = $csv_list[0];
$enclosure = $csv_list[1]; $enclosure = $csv_list[1];
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];

View file

@ -42,6 +42,8 @@ foreach ($csv_lists as $csv_list) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '\\');
$delimiter = $csv_list[0]; $delimiter = $csv_list[0];
$enclosure = $csv_list[1]; $enclosure = $csv_list[1];
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];

View file

@ -45,6 +45,8 @@ foreach ($csv_lists as $csv_list) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '\\');
$delimiter = $csv_list[0]; $delimiter = $csv_list[0];
$enclosure = $csv_list[1]; $enclosure = $csv_list[1];
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];

View file

@ -45,6 +45,8 @@ foreach ($csv_lists as $csv_list) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '\\');
$delimiter = $csv_list[0]; $delimiter = $csv_list[0];
$enclosure = $csv_list[1]; $enclosure = $csv_list[1];
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];

View file

@ -45,6 +45,8 @@ foreach ($csv_lists as $csv_list) {
} else { } else {
$fo = new SplFileObject($file, $file_modes[$mode_counter]); $fo = new SplFileObject($file, $file_modes[$mode_counter]);
} }
$fo->setCsvControl(escape: '\\');
$delimiter = $csv_list[0]; $delimiter = $csv_list[0];
$enclosure = $csv_list[1]; $enclosure = $csv_list[1];
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];

View file

@ -23,7 +23,8 @@ foreach ($s as $row) {
<?php <?php
unlink('csv_control_data_variation001.csv'); unlink('csv_control_data_variation001.csv');
?> ?>
--EXPECT-- --EXPECTF--
Deprecated: SplFileObject::setCsvControl(): the $escape parameter must be provided as its default value will change in %s on line %d
groene appelen : 10 groene appelen : 10
gele bananen : 20 gele bananen : 20
rode kersen : 30 rode kersen : 30

View file

@ -3,6 +3,7 @@ Bug #46569 (SplFileObject: fgetcsv after seek returns wrong line)
--FILE-- --FILE--
<?php <?php
$file = new SplFileObject(__DIR__ . '/bug46569.csv'); $file = new SplFileObject(__DIR__ . '/bug46569.csv');
$file->setCsvControl(escape: "");
$file->seek(1); $file->seek(1);
print_r($file->fgetcsv()); print_r($file->fgetcsv());
?> ?>

View file

@ -8,6 +8,7 @@ $expected = [
]; ];
$tmp = new SplTempFileObject(); $tmp = new SplTempFileObject();
$tmp->setCsvControl(escape: "");
foreach ($expected as $row) { foreach ($expected as $row) {
$tmp->fputcsv($row); $tmp->fputcsv($row);
} }

View file

@ -4,6 +4,7 @@ Bug #77024 SplFileObject::__toString() may return array
<?php <?php
$file = new SplTempFileObject; $file = new SplTempFileObject;
$file->setCsvControl(escape: "");
$file->fputcsv(['foo', 'bar', 'baz']); $file->fputcsv(['foo', 'bar', 'baz']);
$file->rewind(); $file->rewind();
$file->setFlags(SplFileObject::READ_CSV); $file->setFlags(SplFileObject::READ_CSV);

View file

@ -3,6 +3,7 @@ Bug #78976 (SplFileObject::fputcsv returns -1 on failure)
--FILE-- --FILE--
<?php <?php
$file = new SplFileObject('php://memory', 'r'); $file = new SplFileObject('php://memory', 'r');
$file->setCsvControl(escape: "");
var_dump($file->fputcsv(['foo', 'bar'])); var_dump($file->fputcsv(['foo', 'bar']));
?> ?>
--EXPECT-- --EXPECT--

View file

@ -3,9 +3,8 @@ SplFileObject: fgetcsv() on a blank line
--FILE-- --FILE--
<?php <?php
$file_path = __DIR__ . '/SplFileObject_fgetcsv_empty.csv';
$file = new SplFileObject($file_path, 'w');
$file = new SplTempFileObject(); $file = new SplTempFileObject();
$file->setCsvControl(escape: "");
// write to file // write to file
$file->fwrite(""); $file->fwrite("");
@ -18,11 +17,6 @@ $file->setFlags(SplFileObject::SKIP_EMPTY);
$file->rewind(); $file->rewind();
var_dump($file->fgetcsv()); var_dump($file->fgetcsv());
?> ?>
--CLEAN--
<?php
$file_path = __DIR__ . '/SplFileObject_fgetcsv_empty.csv';
unlink($file_path);
?>
--EXPECT-- --EXPECT--
array(1) { array(1) {
[0]=> [0]=>

View file

@ -12,6 +12,7 @@ CSV;
file_put_contents($filename, $csv); file_put_contents($filename, $csv);
$file = new SplFileObject($filename); $file = new SplFileObject($filename);
$file->setCsvControl(escape: "");
$file->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE | SplFileObject::READ_CSV); $file->setFlags(SplFileObject::SKIP_EMPTY | SplFileObject::DROP_NEW_LINE | SplFileObject::READ_CSV);
var_dump(iterator_to_array($file)); var_dump(iterator_to_array($file));

View file

@ -1677,6 +1677,28 @@ quit_loop:
} }
/* }}} */ /* }}} */
PHPAPI int php_csv_handle_escape_argument(const zend_string *escape_str, uint32_t arg_num)
{
if (escape_str != NULL) {
if (ZSTR_LEN(escape_str) > 1) {
zend_argument_value_error(arg_num, "must be empty or a single character");
return PHP_CSV_ESCAPE_ERROR;
}
if (ZSTR_LEN(escape_str) < 1) {
return PHP_CSV_NO_ESCAPE;
} else {
/* use first character from string */
return (unsigned char) ZSTR_VAL(escape_str)[0];
}
} else {
php_error_docref(NULL, E_DEPRECATED, "the $escape parameter must be provided as its default value will change");
if (UNEXPECTED(EG(exception))) {
return PHP_CSV_ESCAPE_ERROR;
}
return (unsigned char) '\\';
}
}
#define FPUTCSV_FLD_CHK(c) memchr(ZSTR_VAL(field_str), c, ZSTR_LEN(field_str)) #define FPUTCSV_FLD_CHK(c) memchr(ZSTR_VAL(field_str), c, ZSTR_LEN(field_str))
/* {{{ Format line as CSV and write to file pointer */ /* {{{ Format line as CSV and write to file pointer */
@ -1684,12 +1706,12 @@ PHP_FUNCTION(fputcsv)
{ {
char delimiter = ','; /* allow this to be set as parameter */ char delimiter = ','; /* allow this to be set as parameter */
char enclosure = '"'; /* allow this to be set as parameter */ char enclosure = '"'; /* allow this to be set as parameter */
int escape_char = (unsigned char) '\\'; /* allow this to be set as parameter */
php_stream *stream; php_stream *stream;
zval *fp = NULL, *fields = NULL; zval *fp = NULL, *fields = NULL;
ssize_t ret; ssize_t ret;
char *delimiter_str = NULL, *enclosure_str = NULL, *escape_str = NULL; char *delimiter_str = NULL, *enclosure_str = NULL;
size_t delimiter_str_len = 0, enclosure_str_len = 0, escape_str_len = 0; zend_string *escape_str = NULL;
size_t delimiter_str_len = 0, enclosure_str_len = 0;
zend_string *eol_str = NULL; zend_string *eol_str = NULL;
ZEND_PARSE_PARAMETERS_START(2, 6) ZEND_PARSE_PARAMETERS_START(2, 6)
@ -1698,7 +1720,7 @@ PHP_FUNCTION(fputcsv)
Z_PARAM_OPTIONAL Z_PARAM_OPTIONAL
Z_PARAM_STRING(delimiter_str, delimiter_str_len) Z_PARAM_STRING(delimiter_str, delimiter_str_len)
Z_PARAM_STRING(enclosure_str, enclosure_str_len) Z_PARAM_STRING(enclosure_str, enclosure_str_len)
Z_PARAM_STRING(escape_str, escape_str_len) Z_PARAM_STR(escape_str)
Z_PARAM_STR_OR_NULL(eol_str) Z_PARAM_STR_OR_NULL(eol_str)
ZEND_PARSE_PARAMETERS_END(); ZEND_PARSE_PARAMETERS_END();
@ -1722,22 +1744,10 @@ PHP_FUNCTION(fputcsv)
enclosure = *enclosure_str; enclosure = *enclosure_str;
} }
if (escape_str != NULL) { int escape_char = php_csv_handle_escape_argument(escape_str, 5);
if (escape_str_len > 1) { if (escape_char == PHP_CSV_ESCAPE_ERROR) {
zend_argument_value_error(5, "must be empty or a single character");
RETURN_THROWS(); RETURN_THROWS();
} }
if (escape_str_len < 1) {
escape_char = PHP_CSV_NO_ESCAPE;
} else {
php_error_docref(NULL, E_DEPRECATED, "Passing a non-empty string to the $escape parameter is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
/* use first character from string */
escape_char = (unsigned char) *escape_str;
}
}
PHP_STREAM_FROM_ZVAL(stream, fp); PHP_STREAM_FROM_ZVAL(stream, fp);
@ -1819,22 +1829,19 @@ PHP_FUNCTION(fgetcsv)
{ {
char delimiter = ','; /* allow this to be set as parameter */ char delimiter = ','; /* allow this to be set as parameter */
char enclosure = '"'; /* allow this to be set as parameter */ char enclosure = '"'; /* allow this to be set as parameter */
int escape = (unsigned char) '\\';
zend_long len = 0; zend_long len = 0;
size_t buf_len; size_t buf_len;
char *buf; char *buf;
php_stream *stream; php_stream *stream;
{
zval *fd; zval *fd;
bool len_is_null = 1; bool len_is_null = 1;
char *delimiter_str = NULL; char *delimiter_str = NULL;
size_t delimiter_str_len = 0; size_t delimiter_str_len = 0;
char *enclosure_str = NULL; char *enclosure_str = NULL;
size_t enclosure_str_len = 0; size_t enclosure_str_len = 0;
char *escape_str = NULL; zend_string *escape_str = NULL;
size_t escape_str_len = 0;
ZEND_PARSE_PARAMETERS_START(1, 5) ZEND_PARSE_PARAMETERS_START(1, 5)
Z_PARAM_RESOURCE(fd) Z_PARAM_RESOURCE(fd)
@ -1842,7 +1849,7 @@ PHP_FUNCTION(fgetcsv)
Z_PARAM_LONG_OR_NULL(len, len_is_null) Z_PARAM_LONG_OR_NULL(len, len_is_null)
Z_PARAM_STRING(delimiter_str, delimiter_str_len) Z_PARAM_STRING(delimiter_str, delimiter_str_len)
Z_PARAM_STRING(enclosure_str, enclosure_str_len) Z_PARAM_STRING(enclosure_str, enclosure_str_len)
Z_PARAM_STRING(escape_str, escape_str_len) Z_PARAM_STR(escape_str)
ZEND_PARSE_PARAMETERS_END(); ZEND_PARSE_PARAMETERS_END();
if (delimiter_str != NULL) { if (delimiter_str != NULL) {
@ -1866,23 +1873,11 @@ PHP_FUNCTION(fgetcsv)
enclosure = enclosure_str[0]; enclosure = enclosure_str[0];
} }
if (escape_str != NULL) { int escape_char = php_csv_handle_escape_argument(escape_str, 5);
if (escape_str_len > 1) { if (escape_char == PHP_CSV_ESCAPE_ERROR) {
zend_argument_value_error(5, "must be empty or a single character");
RETURN_THROWS(); RETURN_THROWS();
} }
if (escape_str_len < 1) {
escape = PHP_CSV_NO_ESCAPE;
} else {
php_error_docref(NULL, E_DEPRECATED, "Passing a non-empty string to the $escape parameter is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
escape = (unsigned char) escape_str[0];
}
}
if (len_is_null || len == 0) { if (len_is_null || len == 0) {
len = -1; len = -1;
} else if (len < 0 || len > (ZEND_LONG_MAX - 1)) { } else if (len < 0 || len > (ZEND_LONG_MAX - 1)) {
@ -1891,7 +1886,6 @@ PHP_FUNCTION(fgetcsv)
} }
PHP_STREAM_FROM_ZVAL(stream, fd); PHP_STREAM_FROM_ZVAL(stream, fd);
}
if (len < 0) { if (len < 0) {
if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) { if ((buf = php_stream_get_line(stream, NULL, 0, &buf_len)) == NULL) {
@ -1905,7 +1899,7 @@ PHP_FUNCTION(fgetcsv)
} }
} }
HashTable *values = php_fgetcsv(stream, delimiter, enclosure, escape, buf_len, buf); HashTable *values = php_fgetcsv(stream, delimiter, enclosure, escape_char, buf_len, buf);
if (values == NULL) { if (values == NULL) {
values = php_bc_fgetcsv_empty_line(); values = php_bc_fgetcsv_empty_line();
} }

View file

@ -47,7 +47,10 @@ PHPAPI void php_flock_common(php_stream *stream, zend_long operation, uint32_t o
zval *wouldblock, zval *return_value); zval *wouldblock, zval *return_value);
#define PHP_CSV_NO_ESCAPE EOF #define PHP_CSV_NO_ESCAPE EOF
#define PHP_CSV_ESCAPE_ERROR -500
PHPAPI HashTable *php_bc_fgetcsv_empty_line(void); PHPAPI HashTable *php_bc_fgetcsv_empty_line(void);
PHPAPI int php_csv_handle_escape_argument(const zend_string *escape_str, uint32_t arg_num);
PHPAPI HashTable *php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf); PHPAPI HashTable *php_fgetcsv(php_stream *stream, char delimiter, char enclosure, int escape_char, size_t buf_len, char *buf);
PHPAPI ssize_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char, zend_string *eol_str); PHPAPI ssize_t php_fputcsv(php_stream *stream, zval *fields, char delimiter, char enclosure, int escape_char, zend_string *eol_str);

View file

@ -5416,16 +5416,16 @@ PHP_FUNCTION(str_getcsv)
{ {
zend_string *str; zend_string *str;
char delimiter = ',', enclosure = '"'; char delimiter = ',', enclosure = '"';
int escape = (unsigned char) '\\'; char *delimiter_str = NULL, *enclosure_str = NULL;
char *delimiter_str = NULL, *enclosure_str = NULL, *escape_str = NULL; size_t delimiter_str_len = 0, enclosure_str_len = 0;
size_t delimiter_str_len = 0, enclosure_str_len = 0, escape_str_len = 0; zend_string *escape_str = NULL;
ZEND_PARSE_PARAMETERS_START(1, 4) ZEND_PARSE_PARAMETERS_START(1, 4)
Z_PARAM_STR(str) Z_PARAM_STR(str)
Z_PARAM_OPTIONAL Z_PARAM_OPTIONAL
Z_PARAM_STRING(delimiter_str, delimiter_str_len) Z_PARAM_STRING(delimiter_str, delimiter_str_len)
Z_PARAM_STRING(enclosure_str, enclosure_str_len) Z_PARAM_STRING(enclosure_str, enclosure_str_len)
Z_PARAM_STRING(escape_str, escape_str_len) Z_PARAM_STR(escape_str)
ZEND_PARSE_PARAMETERS_END(); ZEND_PARSE_PARAMETERS_END();
if (delimiter_str != NULL) { if (delimiter_str != NULL) {
@ -5445,24 +5445,13 @@ PHP_FUNCTION(str_getcsv)
/* use first character from string */ /* use first character from string */
enclosure = enclosure_str[0]; enclosure = enclosure_str[0];
} }
if (escape_str != NULL) {
if (escape_str_len > 1) { int escape_char = php_csv_handle_escape_argument(escape_str, 4);
zend_argument_value_error(4, "must be empty or a single character"); if (escape_char == PHP_CSV_ESCAPE_ERROR) {
RETURN_THROWS(); RETURN_THROWS();
} }
if (escape_str_len < 1) { HashTable *values = php_fgetcsv(NULL, delimiter, enclosure, escape_char, ZSTR_LEN(str), ZSTR_VAL(str));
escape = PHP_CSV_NO_ESCAPE;
} else {
php_error_docref(NULL, E_DEPRECATED, "Passing a non-empty string to the $escape parameter is deprecated since 8.4");
if (UNEXPECTED(EG(exception))) {
RETURN_THROWS();
}
escape = (unsigned char) escape_str[0];
}
}
HashTable *values = php_fgetcsv(NULL, delimiter, enclosure, escape, ZSTR_LEN(str), ZSTR_VAL(str));
if (values == NULL) { if (values == NULL) {
values = php_bc_fgetcsv_empty_line(); values = php_bc_fgetcsv_empty_line();
} }

View file

@ -3,7 +3,7 @@ Bug #12556 (fgetcsv() ignores lengths when quotes not closed)
--FILE-- --FILE--
<?php <?php
$fp = fopen(__DIR__."/test.csv", "r"); $fp = fopen(__DIR__."/test.csv", "r");
while($line = fgetcsv($fp, 24)) { while($line = fgetcsv($fp, 24, escape: "\\")) {
$line = str_replace("\x0d\x0a", "\x0a", $line); $line = str_replace("\x0d\x0a", "\x0a", $line);
var_dump($line); var_dump($line);
} }

View file

@ -3,7 +3,7 @@ Bug #22382 (fgetcsv() does not handle escaped quotes correctly)
--FILE-- --FILE--
<?php <?php
$fp = fopen(__DIR__."/test2.csv", "r"); $fp = fopen(__DIR__."/test2.csv", "r");
while(($line = fgetcsv($fp, 1024))) { while(($line = fgetcsv($fp, 1024, escape: "\\"))) {
var_dump($line); var_dump($line);
} }
fclose($fp); fclose($fp);

View file

@ -3,7 +3,7 @@ Bug #26003 (fgetcsv() is not binary-safe)
--FILE-- --FILE--
<?php <?php
$fp = fopen(__DIR__.'/test3.csv', 'r'); $fp = fopen(__DIR__.'/test3.csv', 'r');
var_dump(fgetcsv($fp, 4096)); var_dump(fgetcsv($fp, 4096, escape: "\\"));
?> ?>
--EXPECTF-- --EXPECTF--
array(2) { array(2) {

View file

@ -2,38 +2,53 @@
Bug #39538 (fgetcsv can't handle starting newlines and trailing odd number of backslashes) Bug #39538 (fgetcsv can't handle starting newlines and trailing odd number of backslashes)
--FILE-- --FILE--
<?php <?php
$content = array("\"\nthis is an test\", \"next data\", \"p\narsed\"","\"\r\nthis is an test\", \"next data\", \"p\r\narsed\"","\"\n\rthis is an test\", \"next data\", \"p\n\rarsed\"");
$file = __DIR__ . "/bug39538.csv"; $file = __DIR__ . "/bug39538.csv";
$line = "\"\nthis is a test\", \"next data\", \"p\narsed\"";
$expectation = [
"\nthis is a test",
"next data",
"p\narsed",
];
file_put_contents($file, $line);
$parsed_content = fgetcsv(fopen($file, "r"), filesize($file), escape: "\\");
var_dump($parsed_content === $expectation);
@unlink($file); @unlink($file);
foreach ($content as $v) {
file_put_contents($file, $v); $line = "\"\r\nthis is a test\", \"next data\", \"p\r\narsed\"";
print_r (fgetcsv(fopen($file, "r"), filesize($file))); $expectation = [
} "\r\nthis is a test",
"next data",
"p\r\narsed",
];
file_put_contents($file, $line);
$parsed_content = fgetcsv(fopen($file, "r"), filesize($file), escape: "\\");
var_dump($parsed_content === $expectation);
@unlink($file);
$line = "\"\n\rthis is a test\", \"next data\", \"p\n\rarsed\"";
$expectation = [
"\n\rthis is a test",
"next data",
"p\n\rarsed",
];
file_put_contents($file, $line);
$parsed_content = fgetcsv(fopen($file, "r"), filesize($file), escape: "\\");
var_dump($parsed_content === $expectation);
?>
--CLEAN--
<?php
$file = __DIR__ . "/bug39538.csv";
@unlink($file); @unlink($file);
?> ?>
--EXPECT-- --EXPECT--
Array bool(true)
( bool(true)
[0] => bool(true)
this is an test
[1] => next data
[2] => p
arsed
)
Array
(
[0] =>
this is an test
[1] => next data
[2] => p
arsed
)
Array
(
[0] =>
this is an test
[1] => next data
[2] => p
arsed
)

View file

@ -11,7 +11,6 @@ fclose($h);
var_dump($data); var_dump($data);
?> ?>
--EXPECTF-- --EXPECTF--
Deprecated: fgetcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
array(2) { array(2) {
[0]=> [0]=>
string(%d) "this element contains the delimiter, and ends with an odd number of string(%d) "this element contains the delimiter, and ends with an odd number of

View file

@ -6,7 +6,7 @@ $file = __DIR__ . "/bug53848.csv";
@unlink($file); @unlink($file);
file_put_contents($file, "a,b\n c, d"); file_put_contents($file, "a,b\n c, d");
$fp = fopen($file, "r"); $fp = fopen($file, "r");
while ($l = fgetcsv($fp)) var_dump($l); while ($l = fgetcsv($fp, escape: "\\")) var_dump($l);
fclose($fp); fclose($fp);
@unlink($file); @unlink($file);
?> ?>

View file

@ -9,13 +9,13 @@ and neither should return FALSE.
$s = fopen("php://memory", "w+"); $s = fopen("php://memory", "w+");
fwrite($s, "\",bar"); fwrite($s, "\",bar");
rewind($s); rewind($s);
var_dump(fgetcsv($s)); var_dump(fgetcsv($s, escape: "\\"));
fclose($s); fclose($s);
$s = fopen("php://memory", "w+"); $s = fopen("php://memory", "w+");
fwrite($s, "\",bar\n"); fwrite($s, "\",bar\n");
rewind($s); rewind($s);
var_dump(fgetcsv($s)); var_dump(fgetcsv($s, escape: "\\"));
fclose($s); fclose($s);
?> ?>
--EXPECT-- --EXPECT--

View file

@ -18,7 +18,6 @@ $fields = str_getcsv($string, ';', '"', "#");
var_dump($fields); var_dump($fields);
?> ?>
--EXPECTF-- --EXPECTF--
Deprecated: str_getcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
array(2) { array(2) {
[0]=> [0]=>
string(11) "first #с؀" string(11) "first #с؀"

View file

@ -34,7 +34,7 @@ various fgetcsv() functionality tests
fwrite($fp, $v); fwrite($fp, $v);
fclose($fp); fclose($fp);
var_dump(fgetcsv(fopen($file, "r"), 1024)); var_dump(fgetcsv(fopen($file, "r"), 1024, escape: "\\"));
} }
@unlink($file); @unlink($file);
?> ?>

View file

@ -0,0 +1,27 @@
--TEST--
fgetcsv(): Deprecation if using default escape arg
--FILE--
<?php
$line = "test1,test2";
$file = __DIR__ . '/fgetcsv_default_escape_deprecation.csv';
$fp = fopen($file, "w");
fwrite($fp, $line);
fclose($fp);
var_dump(fgetcsv(fopen($file, "r"), 1024));
?>
--CLEAN--
<?php
$file = __DIR__ . '/fgetcsv_default_escape_deprecation.csv';
@unlink($file);
?>
--EXPECTF--
Deprecated: fgetcsv(): the $escape parameter must be provided as its default value will change in %s on line %d
array(2) {
[0]=>
string(5) "test1"
[1]=>
string(5) "test2"
}

View file

@ -12,38 +12,38 @@ $enclosure = '"';
echo 'fgetcsv() with negative length' . \PHP_EOL; echo 'fgetcsv() with negative length' . \PHP_EOL;
try { try {
var_dump( fgetcsv($file_handle, -10) ); var_dump( fgetcsv($file_handle, -10, escape: "\\") );
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL; echo $e->getMessage() . \PHP_EOL;
} }
try { try {
var_dump( fgetcsv($file_handle, -10, $delimiter) ); var_dump( fgetcsv($file_handle, -10, $delimiter, escape: "\\") );
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL; echo $e->getMessage() . \PHP_EOL;
} }
try { try {
var_dump( fgetcsv($file_handle, -10, $delimiter, $enclosure) ); var_dump( fgetcsv($file_handle, -10, $delimiter, $enclosure, escape: "\\") );
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL; echo $e->getMessage() . \PHP_EOL;
} }
echo 'fgetcsv() with delimiter as empty string' . \PHP_EOL; echo 'fgetcsv() with delimiter as empty string' . \PHP_EOL;
try { try {
var_dump( fgetcsv($file_handle, $length, '', $enclosure) ); var_dump( fgetcsv($file_handle, $length, '', $enclosure, escape: "\\") );
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL; echo $e->getMessage() . \PHP_EOL;
} }
echo 'fgetcsv() with enclosure as empty string' . \PHP_EOL; echo 'fgetcsv() with enclosure as empty string' . \PHP_EOL;
try { try {
var_dump( fgetcsv($file_handle, $length, $delimiter, '') ); var_dump( fgetcsv($file_handle, $length, $delimiter, '', escape: "\\") );
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL; echo $e->getMessage() . \PHP_EOL;
} }
echo 'fgetcsv() with delimiter & enclosure as empty string' . \PHP_EOL; echo 'fgetcsv() with delimiter & enclosure as empty string' . \PHP_EOL;
try { try {
var_dump( fgetcsv($file_handle, $length, '', '') ); var_dump( fgetcsv($file_handle, $length, '', '', escape: "\\") );
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . \PHP_EOL; echo $e->getMessage() . \PHP_EOL;
} }

View file

@ -2,9 +2,10 @@
fgetcsv() with tab delimited fields (BUG #8258) fgetcsv() with tab delimited fields (BUG #8258)
--FILE-- --FILE--
<?php <?php
chdir(__DIR__);
$fp=fopen("004.data","r"); $file = __DIR__ . '/fgetcsv_tab_delimiter.data';
while($a=fgetcsv($fp,100,"\t")) { $fp = fopen($file,"r");
while ($a = fgetcsv($fp, 100, "\t", escape: '')) {
echo join(",",$a)."\n"; echo join(",",$a)."\n";
} }
fclose($fp); fclose($fp);

View file

@ -72,7 +72,7 @@ foreach ($csv_lists as $csv_list) {
// call fgetcsv() to parse csv fields // call fgetcsv() to parse csv fields
// use the right delimiter and enclosure with max length // use the right delimiter and enclosure with max length
var_dump( fgetcsv($file_handle, 1024, $delimiter, $enclosure) ); var_dump( fgetcsv($file_handle, 1024, $delimiter, $enclosure, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -67,11 +67,11 @@ foreach ($csv_lists as $csv_list) {
// now file pointer should point to end of the file, try reading again // now file pointer should point to end of the file, try reading again
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
var_dump( fgetcsv($file_handle, 1024, $delimiter, $enclosure) ); var_dump( fgetcsv($file_handle, 1024, $delimiter, $enclosure, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
var_dump( fgetcsv($file_handle) ); // with default args var_dump( fgetcsv($file_handle, escape: "\\") ); // with default args
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -71,7 +71,7 @@ foreach ($csv_lists as $csv_list) {
// use different delimiter but same enclosure char // use different delimiter but same enclosure char
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
$enc = "+"; $enc = "+";
var_dump( fgetcsv($file_handle, 1024, $delimiter, $enc) ); var_dump( fgetcsv($file_handle, 1024, $delimiter, $enc, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -45,7 +45,7 @@ $loop_counter = 1;
// read the line which is without csv fields, provide delimiter and see the working of fgetcsv // read the line which is without csv fields, provide delimiter and see the working of fgetcsv
$fp_pos = ftell($file_handle); $fp_pos = ftell($file_handle);
var_dump( fgetcsv($file_handle) ); var_dump( fgetcsv($file_handle, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -46,12 +46,12 @@ $loop_counter = 1;
// read the next line which is a blank line to see the working of fgetcsv // read the next line which is a blank line to see the working of fgetcsv
$fp_pos = ftell($file_handle); $fp_pos = ftell($file_handle);
var_dump( fgetcsv($file_handle, 1024) ); var_dump( fgetcsv($file_handle, 1024, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
// read again to struck EOF // read again to struck EOF
var_dump( fgetcsv($file_handle, 1024) ); var_dump( fgetcsv($file_handle, 1024, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -61,7 +61,7 @@ foreach ($csv_lists as $csv_list) {
// call fgetcsv() to parse csv fields // call fgetcsv() to parse csv fields
var_dump( fgetcsv($file_handle, 1024, $delimiter) ); var_dump( fgetcsv($file_handle, 1024, $delimiter, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -65,7 +65,7 @@ foreach ($csv_lists as $csv_list) {
// use length as 0 // use length as 0
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
var_dump( fgetcsv($file_handle, 0, $delimiter) ); var_dump( fgetcsv($file_handle, 0, $delimiter, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -65,13 +65,13 @@ foreach ($csv_lists as $csv_list) {
// use length as less than the actual size of the line // use length as less than the actual size of the line
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
var_dump( fgetcsv($file_handle, 9, $delimiter) ); var_dump( fgetcsv($file_handle, 9, $delimiter, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
// read rest of the line // read rest of the line
var_dump( fgetcsv($file_handle, 1024, $delimiter) ); var_dump( fgetcsv($file_handle, 1024, $delimiter, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -70,7 +70,7 @@ foreach ($csv_lists as $csv_list) {
// use different delimiter than existing in file // use different delimiter than existing in file
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
$del = "+"; $del = "+";
var_dump( fgetcsv($file_handle, 1024, $del) ); var_dump( fgetcsv($file_handle, 1024, $del, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -68,7 +68,7 @@ foreach ($csv_lists as $csv_list) {
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
$del = "++"; $del = "++";
try { try {
var_dump( fgetcsv($file_handle, 1024, $del) ); var_dump( fgetcsv($file_handle, 1024, $del, escape: "\\") );
} catch (ValueError $e) { } catch (ValueError $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
} }

View file

@ -69,7 +69,7 @@ foreach ($csv_lists as $csv_list) {
// use length as 0 // use length as 0
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
var_dump( fgetcsv($file_handle, 0, $delimiter, $enclosure) ); var_dump( fgetcsv($file_handle, 0, $delimiter, $enclosure, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -48,7 +48,7 @@ $loop_counter = 1;
// read the line which is without csv fields, provide delimiter and see the working of fgetcsv // read the line which is without csv fields, provide delimiter and see the working of fgetcsv
$fp_pos = ftell($file_handle); $fp_pos = ftell($file_handle);
var_dump( fgetcsv($file_handle, 1024) ); var_dump( fgetcsv($file_handle, 1024, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -48,7 +48,7 @@ $loop_counter = 1;
// read the line which is a blank line to see the working of fgetcsv // read the line which is a blank line to see the working of fgetcsv
$fp_pos = ftell($file_handle); $fp_pos = ftell($file_handle);
var_dump( fgetcsv($file_handle, 1024, '+') ); var_dump( fgetcsv($file_handle, 1024, '+', escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -66,7 +66,7 @@ foreach ($csv_lists as $csv_list) {
// now file pointer should point to end of the file, try reading again // now file pointer should point to end of the file, try reading again
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
var_dump( fgetcsv($file_handle, 1024, $delimiter) ); // with length, delimiter var_dump( fgetcsv($file_handle, 1024, $delimiter, escape: "\\") ); // with length, delimiter
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -16,13 +16,13 @@ if (!$fp) {
echo "Error: failed to create file $filename!\n"; echo "Error: failed to create file $filename!\n";
exit(); exit();
} }
var_dump( fgetcsv($fp) ); var_dump( fgetcsv($fp, escape: "\\") );
var_dump( ftell($fp) ); var_dump( ftell($fp) );
var_dump( fgetcsv($fp, 1024) ); var_dump( fgetcsv($fp, 1024, escape: "\\") );
var_dump( ftell($fp) ); var_dump( ftell($fp) );
var_dump( fgetcsv($fp, 1024, "+" ) ); var_dump( fgetcsv($fp, 1024, "+", escape: "\\" ) );
var_dump( ftell($fp) ); var_dump( ftell($fp) );
var_dump( fgetcsv($fp, 1024, "+", "%") ); var_dump( fgetcsv($fp, 1024, "+", "%", escape: "\\") );
var_dump( ftell($fp) ); var_dump( ftell($fp) );
// close and delete the file // close and delete the file

View file

@ -67,7 +67,7 @@ foreach ($csv_lists as $csv_list) {
// now file pointer should point to end of the file, try reading again // now file pointer should point to end of the file, try reading again
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
var_dump( fgetcsv($file_handle) ); var_dump( fgetcsv($file_handle, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -66,7 +66,7 @@ foreach ($csv_lists as $csv_list) {
// now file pointer should point to end of the file, try reading again // now file pointer should point to end of the file, try reading again
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
var_dump( fgetcsv($file_handle, 1024) ); var_dump( fgetcsv($file_handle, 1024, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -68,7 +68,7 @@ foreach ($csv_lists as $csv_list) {
// now file pointer should point to end of the file, try reading again // now file pointer should point to end of the file, try reading again
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
$enc = 'z'; $enc = 'z';
var_dump( fgetcsv($file_handle, 1024, $delimiter, $enc ) ); // with length, delimiter var_dump( fgetcsv($file_handle, 1024, $delimiter, $enc, escape: "\\" ) ); // with length, delimiter
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -9,7 +9,7 @@ EOS;
$stream = fopen('php://memory', 'w+'); $stream = fopen('php://memory', 'w+');
fwrite($stream, $contents); fwrite($stream, $contents);
rewind($stream); rewind($stream);
while (($data = fgetcsv($stream)) !== false) { while (($data = fgetcsv($stream, escape: "\\")) !== false) {
var_dump($data); var_dump($data);
} }
fclose($stream); fclose($stream);

View file

@ -71,12 +71,12 @@ foreach ($csv_lists as $csv_list) {
// use length as less than the actual size of the line // use length as less than the actual size of the line
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
var_dump( fgetcsv($file_handle, 9, $delimiter, $enclosure) ); var_dump( fgetcsv($file_handle, 9, $delimiter, $enclosure, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );
// read rest of the line // read rest of the line
var_dump( fgetcsv($file_handle, 1024, $delimiter, $enclosure) ); var_dump( fgetcsv($file_handle, 1024, $delimiter, $enclosure, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -72,7 +72,7 @@ foreach ($csv_lists as $csv_list) {
// use only default arguments // use only default arguments
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
var_dump( fgetcsv($file_handle) ); var_dump( fgetcsv($file_handle, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -74,7 +74,7 @@ foreach ($csv_lists as $csv_list) {
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
$del = "+"; $del = "+";
$enc = "%"; $enc = "%";
var_dump( fgetcsv($file_handle, 1024, $del, $enc) ); var_dump( fgetcsv($file_handle, 1024, $del, $enc, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -76,7 +76,7 @@ foreach ($csv_lists as $csv_list) {
// use different delimiter but same enclosure char // use different delimiter but same enclosure char
fseek($file_handle, 0, SEEK_SET); fseek($file_handle, 0, SEEK_SET);
$del = "+"; $del = "+";
var_dump( fgetcsv($file_handle, 1024, $del, $enclosure) ); var_dump( fgetcsv($file_handle, 1024, $del, $enclosure, escape: "\\") );
// check the file pointer position and if eof // check the file pointer position and if eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -31,7 +31,7 @@ $file = __DIR__ . '/fputcsv.csv';
$fp = fopen($file, "w"); $fp = fopen($file, "w");
foreach ($list as $v) { foreach ($list as $v) {
fputcsv($fp, explode(',', $v)); fputcsv($fp, explode(',', $v), escape: "\\");
} }
fclose($fp); fclose($fp);
@ -44,7 +44,7 @@ echo '$list = ';var_export($res);echo ";\n";
$fp = fopen($file, "r"); $fp = fopen($file, "r");
$res = array(); $res = array();
while($l=fgetcsv($fp)) while($l=fgetcsv($fp, escape: "\\"))
{ {
$res[] = join(',',$l); $res[] = join(',',$l);
} }

View file

@ -9,7 +9,7 @@ $data = array(1, 2, 'foo', 'haha', array(4, 5, 6), 1.3, null);
$fp = fopen($file, 'w'); $fp = fopen($file, 'w');
fputcsv($fp, $data); fputcsv($fp, $data, escape: "\\");
var_dump($data); var_dump($data);

View file

@ -0,0 +1,27 @@
--TEST--
fputcsv(): Deprecation if using default escape arg
--FILE--
<?php
$line = "test1,test2";
$file = __DIR__ . '/fputcsv_default_escape_deprecation.csv';
$data = ["test1", "test2"];
$fp = fopen($file, 'w');
fputcsv($fp, $data);
unset($fp);
var_dump(file_get_contents($file));
?>
--CLEAN--
<?php
$file = __DIR__ . '/fputcsv_default_escape_deprecation.csv';
@unlink($file);
?>
--EXPECTF--
Deprecated: fputcsv(): the $escape parameter must be provided as its default value will change in %s on line %d
string(12) "test1,test2
"

View file

@ -50,7 +50,7 @@ foreach ($csv_lists as $csv_list) {
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];
var_dump( fputcsv($file_handle, $csv_field, $delimiter, $enclosure) ); var_dump( fputcsv($file_handle, $csv_field, $delimiter, $enclosure, escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -42,7 +42,7 @@ foreach ($fields as $field) {
$csv_field = $field; $csv_field = $field;
// write to a file in csv format // write to a file in csv format
var_dump( fputcsv($file_handle, $csv_field) ); var_dump( fputcsv($file_handle, $csv_field, escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );

View file

@ -50,7 +50,7 @@ foreach ($csv_lists as $csv_list) {
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];
// write to a file in csv format // write to a file in csv format
var_dump( fputcsv($file_handle, $csv_field, $delimiter) ); var_dump( fputcsv($file_handle, $csv_field, $delimiter, escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -51,7 +51,7 @@ foreach ($csv_lists as $csv_list) {
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];
// write to a file in csv format // write to a file in csv format
var_dump( fputcsv($file_handle, $csv_field, '+') ); var_dump( fputcsv($file_handle, $csv_field, '+', escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -52,7 +52,7 @@ foreach ($csv_lists as $csv_list) {
// write to a file in csv format // write to a file in csv format
try { try {
var_dump( fputcsv($file_handle, $csv_field, '+', '%%') ); var_dump( fputcsv($file_handle, $csv_field, '+', '%%', escape: "\\") );
} catch (ValueError $e) { } catch (ValueError $e) {
echo $e->getMessage(), "\n"; echo $e->getMessage(), "\n";
} }

View file

@ -42,29 +42,12 @@ $file = __DIR__ . '/fputcsv_variation18.csv';
@unlink($file); @unlink($file);
?> ?>
--EXPECTF-- --EXPECTF--
Deprecated: fputcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
Deprecated: fputcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
Deprecated: fputcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
Deprecated: fputcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
$list = array ( $list = array (
0 => 'aaa,"""/"bbb",ccc', 0 => 'aaa,"""/"bbb",ccc',
1 => '"aaa""/"a""","""bbb"""', 1 => '"aaa""/"a""","""bbb"""',
2 => '"""/"""","""aaa"""', 2 => '"""/"""","""aaa"""',
3 => '"""/"""""",aaa', 3 => '"""/"""""",aaa',
); );
Deprecated: fgetcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
Deprecated: fgetcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
Deprecated: fgetcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
Deprecated: fgetcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
Deprecated: fgetcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
$list = array ( $list = array (
0 => 'aaa,"/"bbb,ccc', 0 => 'aaa,"/"bbb,ccc',
1 => 'aaa"/"a","bbb"', 1 => 'aaa"/"a","bbb"',

View file

@ -50,7 +50,7 @@ foreach ($csv_lists as $csv_list) {
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];
// write to a file in csv format // write to a file in csv format
var_dump( fputcsv($file_handle, $csv_field) ); var_dump( fputcsv($file_handle, $csv_field, escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -53,7 +53,7 @@ foreach ($csv_lists as $csv_list) {
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];
// write to a file in csv format // write to a file in csv format
var_dump( fputcsv($file_handle, $csv_field, '+', '%') ); var_dump( fputcsv($file_handle, $csv_field, '+', '%', escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -53,7 +53,7 @@ foreach ($csv_lists as $csv_list) {
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];
// write to a file in csv format // write to a file in csv format
var_dump( fputcsv($file_handle, $csv_field, '+', $enclosure) ); var_dump( fputcsv($file_handle, $csv_field, '+', $enclosure, escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -53,7 +53,7 @@ foreach ($csv_lists as $csv_list) {
$csv_field = $csv_list[2]; $csv_field = $csv_list[2];
// write to a file in csv format // write to a file in csv format
var_dump( fputcsv($file_handle, $csv_field, $delimiter, '+') ); var_dump( fputcsv($file_handle, $csv_field, $delimiter, '+', escape: "\\") );
// check the file pointer position and eof // check the file pointer position and eof
var_dump( ftell($file_handle) ); var_dump( ftell($file_handle) );
var_dump( feof($file_handle) ); var_dump( feof($file_handle) );

View file

@ -7,12 +7,12 @@ touch($filename);
$fp = fopen ($filename, "r"); $fp = fopen ($filename, "r");
try { try {
fgetcsv($fp, PHP_INT_MAX); fgetcsv($fp, PHP_INT_MAX, escape: '');
} catch (\ValueError $e) { } catch (\ValueError $e) {
echo $e->getMessage() . PHP_EOL; echo $e->getMessage() . PHP_EOL;
} }
fgetcsv($fp, PHP_INT_MAX-1); fgetcsv($fp, PHP_INT_MAX-1, escape: '');
--CLEAN-- --CLEAN--
<?php <?php
@unlink(__DIR__ . "/gh15653.tmp"); @unlink(__DIR__ . "/gh15653.tmp");

View file

@ -2,10 +2,10 @@
Bug #55674 (fgetcsv & str_getcsv skip empty fields in some tab-separated records) Bug #55674 (fgetcsv & str_getcsv skip empty fields in some tab-separated records)
--FILE-- --FILE--
<?php <?php
var_dump(str_getcsv("0\t\t\"2\"\n", "\t")); var_dump(str_getcsv("0\t\t\"2\"\n", "\t", escape: ''));
var_dump(str_getcsv("0\t \t'2'\n", "\t", "'")); var_dump(str_getcsv("0\t \t'2'\n", "\t", "'", escape: ''));
var_dump(str_getcsv(",,,,")); var_dump(str_getcsv(",,,,", escape: ''));
var_dump(str_getcsv(" \t \t\t\t ", "\t")); var_dump(str_getcsv(" \t \t\t\t ", "\t", escape: ''));
?> ?>
--EXPECT-- --EXPECT--
array(3) { array(3) {

View file

@ -5,7 +5,7 @@ Bug #65947 (basename is no more working after fgetcsv in certain situation)
$filename = 'test.toto'; $filename = 'test.toto';
// é in ISO-8859-1 // é in ISO-8859-1
$csv = base64_decode('6Q=='); $csv = base64_decode('6Q==');
$adata = str_getcsv($csv,";"); $adata = str_getcsv($csv,";", escape: '');
$b2 = basename($filename); $b2 = basename($filename);
if ($filename != $b2) if ($filename != $b2)
print "BUG"; print "BUG";

View file

@ -2,9 +2,9 @@
GH-11982 (str_getcsv returns null byte for unterminated quoted string) GH-11982 (str_getcsv returns null byte for unterminated quoted string)
--FILE-- --FILE--
<?php <?php
var_dump(str_getcsv('"')); var_dump(str_getcsv('"', escape: ''));
var_dump(str_getcsv('"field","')); var_dump(str_getcsv('"field","', escape: ''));
var_dump(str_getcsv('"","a')); var_dump(str_getcsv('"","a', escape: ''));
?> ?>
--EXPECT-- --EXPECT--
array(1) { array(1) {

View file

@ -3,14 +3,13 @@ GH-12151 (str_getcsv ending with escape zero segfualt)
--FILE-- --FILE--
<?php <?php
var_export(str_getcsv("y",",","y","\000")); var_export(str_getcsv("y",",","y","\000"));
echo "\n";
var_export(str_getcsv("\0yy","y","y","\0")); var_export(str_getcsv("\0yy","y","y","\0"));
?> ?>
--EXPECTF-- --EXPECT--
Deprecated: str_getcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
array ( array (
0 => '' . "\0" . '', 0 => '' . "\0" . '',
) )
Deprecated: str_getcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
array ( array (
0 => '' . "\0" . '', 0 => '' . "\0" . '',
1 => '' . "\0" . '', 1 => '' . "\0" . '',

View file

@ -6,6 +6,7 @@ var_dump(str_getcsv(
"aaaaaaaaaaaa\0 ", "aaaaaaaaaaaa\0 ",
"\0", "\0",
"\0", "\0",
escape: '',
)); ));
?> ?>
--EXPECT-- --EXPECT--

View file

@ -4,13 +4,13 @@ str_getcsv(): Testing using various arguments
<?php <?php
// string input[, string delimiter[, string enclosure[, string escape]]] // string input[, string delimiter[, string enclosure[, string escape]]]
var_dump(str_getcsv('"f", "o", ""')); var_dump(str_getcsv('"f", "o", ""', escape: ''));
print "-----\n"; print "-----\n";
var_dump(str_getcsv('foo||bar', '|')); var_dump(str_getcsv('foo||bar', '|', escape: ''));
print "-----\n"; print "-----\n";
var_dump(str_getcsv('foo|bar', '|')); var_dump(str_getcsv('foo|bar', '|', escape: ''));
print "-----\n"; print "-----\n";
var_dump(str_getcsv('|foo|-|bar|', '-', '|')); var_dump(str_getcsv('|foo|-|bar|', '-', '|', escape: ''));
print "-----\n"; print "-----\n";
var_dump(str_getcsv('|f.|.|bar|.|-|-.|', '.', '|', '-')); var_dump(str_getcsv('|f.|.|bar|.|-|-.|', '.', '|', '-'));
print "-----\n"; print "-----\n";
@ -18,9 +18,9 @@ var_dump(str_getcsv('.foo..bar.', '.', '.', '.'));
print "-----\n"; print "-----\n";
var_dump(str_getcsv('.foo . . bar .', ' ', '.', '')); var_dump(str_getcsv('.foo . . bar .', ' ', '.', ''));
print "-----\n"; print "-----\n";
var_dump(str_getcsv('" "" "', ' ')); var_dump(str_getcsv('" "" "', ' ', escape: ''));
print "-----\n"; print "-----\n";
var_dump(str_getcsv('')); var_dump(str_getcsv('', escape: ''));
print "-----\n"; print "-----\n";
?> ?>
@ -57,8 +57,6 @@ array(2) {
string(3) "bar" string(3) "bar"
} }
----- -----
Deprecated: str_getcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
array(3) { array(3) {
[0]=> [0]=>
string(2) "f." string(2) "f."
@ -68,8 +66,6 @@ array(3) {
string(4) "-|-." string(4) "-|-."
} }
----- -----
Deprecated: str_getcsv(): Passing a non-empty string to the $escape parameter is deprecated since 8.4 in %s on line %d
array(1) { array(1) {
[0]=> [0]=>
string(7) "foo.bar" string(7) "foo.bar"