Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSVインポート時にデリミタがエスケープされていることがある #5252

Closed
izayoi256 opened this issue Dec 10, 2021 · 1 comment
Labels
Milestone

Comments

@izayoi256
Copy link
Contributor

izayoi256 commented Dec 10, 2021

環境 (environment)

  • EC-CUBE: 4.1.0

概要(Overview)

参考: https://sites.google.com/site/fudist/Home/grep/sjis-damemoji-jp/table

sjis.csv

"hoge","テスト機構","fuga"

PHP

$csv = new CsvImportService(new SplFileObject(__DIR__ . '/sjis.csv'));
foreach ($csv as $row) {
    echo count($row), ' ', var_export($row, true), '<br />';
}

実行結果

2 array ( 0 => 'hoge', 1 => '�e�X�g�@�\\",fuga"', )

修正案

CsvImportService::__construct()

    public function __construct(\SplFileObject $file, $delimiter = ',', $enclosure = '"', $escape = '\\')
     {
         ini_set('auto_detect_line_endings', true);
 
-        $this->file = $file;
+        $tempFile = tmpfile();
+        foreach ($file as $line) {
+            $encoded = mb_convert_encoding($line, 'UTF-8', 'SJIS-win');
+            fwrite($tempFile, $encoded);
+        }
+
+        $meta = stream_get_meta_data($tempFile);
+        $this->file = new SplFileObject($meta['uri'], 'r');
+        fclose($tempFile);
+
         $this->file->setFlags(
             \SplFileObject::READ_CSV |
             \SplFileObject::SKIP_EMPTY |

実行結果

3 array ( 0 => 'hoge', 1 => 'テスト機構', 2 => 'fuga', )
@izayoi256 izayoi256 reopened this Dec 10, 2021
@izayoi256 izayoi256 changed the title CSVインポート時にデリミタがエスケープされてしまう CSVインポート時にデリミタがエスケープされてしまうことがある Dec 10, 2021
@izayoi256 izayoi256 changed the title CSVインポート時にデリミタがエスケープされてしまうことがある CSVインポート時にデリミタがエスケープされてe.ことがある Dec 10, 2021
@izayoi256 izayoi256 changed the title CSVインポート時にデリミタがエスケープされてe.ことがある CSVインポート時にデリミタがエスケープされていることがある Dec 10, 2021
@chihiro-adachi chihiro-adachi added this to the 4.1.x milestone Dec 23, 2021
@taguchimasahiro taguchimasahiro modified the milestones: 4.1.x, 4.2.0 Jun 20, 2022
nanasess added a commit to TPS-NghiaLe/ec-cube that referenced this issue Jun 28, 2022
@chihiro-adachi
Copy link
Contributor

#5407 で対応

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants