9

According to the PHP website it does this:

encoding is the character encoding name used for the HTTP input character encoding conversion, HTTP output character encoding conversion, and the default character encoding for string functions defined by the mbstring module. You should notice that the internal encoding is totally different from the one for multibyte regex.

Can someone please explain this in simpler terms?

  1. HTTP input character encoding conversion
  2. HTTP output character encoding conversion
  3. default character encoding for string functions
  4. What is meant by “internal encoding is totally different from the one for multibyte regex”?

My guess is that

  1. means GET and POST are treated as that encoding.
  2. means it outputs to that encoding.
  3. means it uses that encoding for all multibyte string functions.
  4. I have no idea about. Why would regex be different to normal string functions?

If point 2 is correct would you need to do:

ini_set('default_charset', 'UTF-8');

If I understand 3 correctly does that mean if you do:

mb_internal_encoding('UTF-8')

You don't need to do:

mb_strtolower($str, 'UTF-8');

Just:

mb_strtolower($str);

I did read on another SO post that mb_strtolower($str) should no be trusted and that you need to set the encoding for each multibyte string function. Is this true?

1

1 Answer 1

9

The mbstring extension added the glorious idea (</sarcasm>) to automatically convert all incoming data and all output data from some encoding to another. See mbstring HTTP Input and Output. It's configured with the mbstring.http_input ini setting and by using the mb_output_handler. mb_internal_encoding influences this conversion. IMO you should leave those settings off and never touch them; I have yet to find any problem that can elegantly be solved by this and it sounds like a terrible idea overall to have implicit encoding conversions going on. Especially if it's all controlled via one global flag (mb_internal_encoding) which is used in a variety of different contexts.
So that's 1. and 2.

For 3., yes indeed, mb_internal_encoding basically sets the default value for all mb_ functions which accept an $encoding parameter. Essentially it just sets a global variable (internally) which other functions read from, that's all.

The last part refers to the fact that there's a separate mb_regex_encoding function to set the internal encoding for mb_ereg_ functions.

I did read on another SO post that mb_strtolower($str) should no be trusted and that you need to set the encoding for each multibyte string function. Is this true?

I'd agree to this insofar as all global state cannot be trusted. This is pretty trustworthy:

mb_internal_encoding('UTF-8');
mb_strtolower($string);

However, this is not really:

mb_strtolower($string);

See the difference? If you rely on global state being set correctly elsewhere, you can never be sure it actually is correct. You just need to make a call to some third party library which sets mb_internal_encoding to something else without you knowing, and your mb_strtolower call will suddenly behave very differently.

8
  • Thanks. 1, 2. So as long as mbstring.http_input = pass and mbstring.http_output = pass that's fine? 3. I see what you're saying but then in theory any library could also change things with ini_set. Where do you draw the line? 4. Should I be running mb_regex_encoding('UTF-8')? EUC-JP is the default, whatever that is.
    – texelate
    Mar 26, 2014 at 12:23
  • 1, 2: Yes. 3. Meh, that's a never ending question. :) 4. If you're not using any of the mb_ereg functions (and I'm not sure when you would prefer them over preg_), leave it as is.
    – deceze
    Mar 26, 2014 at 13:12
  • Thanks, I'll accept your answer. :-) By the way, do you know what EUC-JP is?
    – texelate
    Mar 26, 2014 at 14:53
  • 1
    Based on anecdotal evidence it appears to me that PHP's encoding facilities largely grew out of a need to support some Japanese developers... Total stab into the dark as to why that may be a default...
    – deceze
    Mar 26, 2014 at 15:56
  • 1
    EUC-JP (Extended Unix Code) has ASCII as subset. That said, unless you wander outside of ASCII, you wouldn't notice any difference.
    – sanmai
    May 10, 2016 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.