0

Good evening I tried every code I've found on the internet to solve this silly problem... I hope you have a solution.

I have a CSV file, whith a field containing € (euro) sign.

Based on Notepad++, the file encoding is ANSI.

I need to parse it and import it into a DB, in a json format.

$path = $request->file('csv_file')->getRealPath();
$delimiter = ';';
$data = array_map(function($str) use ($delimiter) {
  return str_getcsv($str, $delimiter);
}, file($path));

... some stuff ....

$csvDataFile = CsvData::create([
            'csv_filename' => $request->file('csv_file')->getClientOriginalName(),
            'csv_header' => $request->has('header'),
            'csv_data' => json_encode(array_slice($data,1)),
            'csv_info' => json_encode($request->csv_info),
]);

The field csv_data on the DB is 0, because the € sign breaks the json_encode function.

I tried to replace it with an empty char: preg_replace('/\x{20AC}/u', '', $line); scanning the array line by line, but the € sign is no more the same (neither €, nor \x20AC ).

I also tried to read the whole file as a string with `fgets, and replace the € sign there, but it's not explicitly present.

I finally tried the solution reported here fgetcsv() drops characters with diacritics (i.e. non-ASCII) - how to fix? ... but it not seems to work.

Any smart solution?

Thanks!

Simone Conti
  • 349
  • 1
  • 17
  • Not sure if it's a true answer to your question, but when working with imports & exports from/to CSV in combination with Laravel, I've heard good stories about Laravel Excel: https://docs.laravel-excel.com/3.1/imports/ – GlennM May 07 '21 at 19:26
  • 1
    @GlennM That library has some problem with CSV files in version 3.1 I'm testing it now to import XLS files instead. Thanks – Simone Conti May 08 '21 at 08:42

0 Answers0