5

An annoying encoding error worries about a new dataset in a mongoDB insert and stops my script when there is a encoding issue?

       PHP Fatal error: Uncaught exception 'MongoException' with message 'non-utf8 string: ü'

How to fix the new dataset before the PHP driver breaks?

Is there a better idea than utf8_encode any string data, even those that are already utf8?

2 Answers 2

5

Had the same issue. This works:

$string = mb_convert_encoding($string, 'ISO-8859-1', 'UTF-8');
Sign up to request clarification or add additional context in comments.

Comments

2

utf8_encode() ( http://php.net/manual/en/function.utf8-encode.php ) since the default PHP encoding is still not utf8 yet I think (not sure about PHP 5.4).

6 Comments

UTF-8 wont be the default encoding until PHP 6 (whenever that gets released)
utf8_encode() is rarely the answer to anything, it can only cope with input in ISO-8859-1. Use iconv() or mb_convert_encoding() and specify the input data encoding explicitly.
@DaveRandom Sorry realised what you said now
@datasage The feature list for PHP6 is a complete unknown. Anything you read about it online is wrong, the original PHP6 branch was merged back to 5.x a long time ago. Regardless, the input will be in the format in which it was provided - if it's a string literal and the PHP script file is saved as UTF8, the data will be UTF8. If it comes from user input and the input it UTF8, it will be UTF8. Strings in PHP are just byte sequences, they are character set agnostic (which is exactly why mbstring exists).
If my script is not the source of all the data that is inserted, it's pretty hard to iconv/mb_convert_encoding for sure. Why can't all string be ASCII, no special chars, no Umlauts etc. :/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.