Search code examples
phputf-8whitespace

PHP replace weird space


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.


Solution

  • I was able to fix the issue with iconv:

    $name = !empty($data[0]) ? iconv("UTF-8", "ISO-8859-1//IGNORE", $data[0]) : null;