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?
- HTTP input character encoding conversion
- HTTP output character encoding conversion
- default character encoding for string functions
- What is meant by “internal encoding is totally different from the one for multibyte regex”?
My guess is that
- means GET and POST are treated as that encoding.
- means it outputs to that encoding.
- means it uses that encoding for all multibyte string functions.
- I have no idea about. Why would regex be different to normal string functions?
If point 2 is correct would you need to do:
If I understand 3 correctly does that mean if you do:
You don’t need to do:
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?
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_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
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:
However, this is not really:
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.