PHP default string encoding

Can someone explain why the output ASCIIin the last three tests is lower?

I get the same results on my own system, PHPTester.net , and PhpFiddle.org .

echo mb_internal_encoding();                       // UTF-8

$str = 'foobar';
echo mb_check_encoding($str, 'UTF-8');             // true
echo mb_detect_encoding($str);                     // ASCII

$encoded = utf8_encode($str);
echo mb_detect_encoding($encoded);                 // ASCII

$converted = mb_convert_encoding($str, 'UTF-8');
echo mb_detect_encoding($converted);               // ASCII
+4
source share
1 answer

This would be because foobarthere are no characters that cannot be represented in ASCII.

mb_check_encoding($str, 'UTF-8') works because ASCII text is natively compatible with UTF-8 (intentionally)

But in the absence of multibyte characters, there is no distinguishable difference between them. Proof of this:'foobar' === utf8_encode('foobar') // true

+5
source

Source: https://habr.com/ru/post/1677640/


All Articles