|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-06-03 16:00 UTC] cronfy at gmail dot com
Description:
------------
When escape character set for str_getcsv() is followed by some UTF characters, string is parsed incorrectly.
I tested it on php 5.4, 5.5, 5.6 and 7.0 - behavior is the same.
Test script:
---------------
$utf_1 = chr(0xD1) . chr(0x81); // U+0440;
$utf_2 = chr(0xD8) . chr(0x80); // U+0600
$string = '"first #' . $utf_1 . $utf_2 . '";"second one"';
$d = str_getcsv($string, ';', '"', "#");
print_r($d);
Expected result:
----------------
Array
(
[0] => first #с
[1] => second one
)
Actual result:
--------------
Array
(
[0] => first #с";second one"
)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 16:00:01 2025 UTC |
str_getcsv not correctly work with qouted multibyte character PHP version: 7.2.2 Operating system: Windows 10 zh-CN Description: ------------ str_getcsv not correctly work with qouted multibyte characters. When the multibyte characters are simply seperated by comma, everything seems ok. If the value contains a quotation mark("), I need to escape quotation mark by doubled quotation mark(""), and quote the value with a pair of quotation mark. And when I try to decode the csv string by str_getcsv, this value will combined with next value (I lost a column and got two value together in one column). There is not just one type of wrong result. But I think every type of wrong result be caused by the escaped quotation mark. Bug #72330: CSV fields incorrectly split if escape char followed by UTF chars Test script: --------------- <?php // Test 1 $data = [ "\xE4\xBD\xA0\xE5\xA5\xBD", // 你好 "\xE4\xB8\x96\xE7\x95\x8C", // 世界 ]; $encoded = implode(',', array_map(function ($value) { return '"' . str_replace('"', '""', $value) . '"'; }, $data)); var_dump(str_getcsv($encoded) === $data); // Test 2 $data = [ "\"\xE5\x95\x8A", // "啊 ]; $encoded = str_putcsv($data); var_dump(str_getcsv($encoded) === $data); /** @link https://bugs.php.net/bug.php?id=64183 */ function str_putcsv($fields, $delimiter = ',', $enclosure = '"', $escape_char = '\\') { $stream = fopen('php://memory', 'w+'); fputcsv($stream, $fields, $delimiter, $enclosure, $escape_char); rewind($stream); return stream_get_contents($stream); } Expected result: ---------------- bool(true) bool(true) Actual result: -------------- bool(false) bool(false)