cantera
cantera

Reputation: 24995

PHP Default String Encoding

Could someone explain why the output is ASCII in the last three tests below?

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

Upvotes: 4

Views: 370

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

That would be because there are no characters in foobar that cannot be represented in ASCII.

mb_check_encoding($str, 'UTF-8') works because ASCII text is innately compatible with UTF-8 (deliberately so)

But in the absence of multi-byte characters, there's no discernible difference between the two. Proof of this: 'foobar' === utf8_encode('foobar') // true

Upvotes: 5

Related Questions