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!