Mongodb PHP Utf-8 Problems

MongoDB PHP UTF-8 problems

JSON and BSON can only encode / decode valid UTF-8 strings, if your data (included input) is not UTF-8 you need to convert it before passing it to any JSON dependent system, like this:

$string = iconv('UTF-8', 'UTF-8//IGNORE', $string); // or
$string = iconv('UTF-8', 'UTF-8//TRANSLIT', $string); // or even
$string = iconv('UTF-8', 'UTF-8//TRANSLIT//IGNORE', $string); // not sure how this behaves

Personally I prefer the first option, see the iconv() manual page. Other alternatives include:

  • mb_convert_encoding()
  • utf8_encode(utf8_decode($string))

You should always make sure your strings are UTF-8 encoded, even the user-submitted ones, however since you mentioned that you're migrating from MySQL to MongoDB, have you tried exporting your current database to CSV and using the import scripts that come with Mongo? They should handle this...


EDIT: I mentioned that BSON can only handle UTF-8, but I'm not sure if this is exactly true, I have a vague idea that BSON uses UTF-16 or UTF-32 to encode / decode data, but I can't check now.

PHP MongoDB-insert - fatal error with utf8

Had the same issue. This works:

$string = mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');

Mongo db utf-8 exception

I had related problem

eq

ucfirst for UTF-8 need use mb_ucfirst('helo','UTF-8');

And i think in your situation problem is with: substr need use mb_substr

else :

So meybe on the begin iconv convert to iso-8859-1 and on write to db icon to t Utf-8



Related Topics



Leave a reply



Submit