0

I have and Excel file that I converted to a CSV file. Apparently there are two kinds of spaces in this file, the regular space and a non-breaking space (my assumption).

When saving the data to the database the non-breaking space gets saved as a black diamond with a question mark (�).

I've been reading around as this probably has solutions already but when I try them, nothing is happening.

Using the mb_substr approach:

$name = !empty($data[0]) ? mb_substr($data[0], 0, mb_strlen($data[0]), "UTF-8") : null;

Using the str_replace approach:

$name = !empty($data[0]) ? str_replace(" ", " ", $data[0]) : null;

Edit

I'm using Oracle for the database and here are the Character Sets defined:

NLS_CHARACTERSET       = AL32UTF8
NLS_NCHAR_CHARACTERSET = AL16UTF16

Here's a sample data:

, W.R.,

The space before W is the character that gets transformed into the question mark.

halfer
  • 19,824
  • 17
  • 99
  • 186
dokgu
  • 4,957
  • 3
  • 39
  • 77

2 Answers2

0

If you look in your settings for the Excel program and see if you can establish the character set of the original file you can then upload the file and use PHP MB_convert_encoding function to translate from whatever Excel saves it as to UTF-8, or better yet simply export it as UTF-8 directly from Excel.

also be aware that if you're using MySQL the UTF8_ character set is BROKEN and UTF8mb4 should be used instead.

Community
  • 1
  • 1
Martin
  • 22,212
  • 11
  • 70
  • 132
0

I was able to fix the issue with iconv:

$name = !empty($data[0]) ? iconv("UTF-8", "ISO-8859-1//IGNORE", $data[0]) : null;
dokgu
  • 4,957
  • 3
  • 39
  • 77