4

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

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

Comments

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.