Why can't mbstring detect latin1 characters as cp1252?

Viewed 326

ASCII characters are detected as valid latin1, but not cp1252.

mb_detect_encoding("a",["ISO-8859-1"],true);   // "ISO-8859-1"  
mb_detect_encoding("a",["Windows-1252"],true); // false

The additional characters in the range 80-9F are detectable as both:

mb_detect_encoding("\x80",["ISO-8859-1"],true);   // "ISO-8859-1"  
mb_detect_encoding("\x80",["Windows-1252"],true); // "Windows-1252"

But common extended characters are not. Take the é character at 0xE9. PHP detects it as ISO, but not the superset:

mb_detect_encoding("\xE9",["ISO-8859-1"],true);   // "ISO-8859-1"  
mb_detect_encoding("\xE9",["Windows-1252"],true); // false

Converting the additional characters to UTF-8 requires using the Windows charset, which works as expected:

mb_convert_encoding("a\xE9\x80","UTF-8","Windows-1252"); // aé€
mb_convert_encoding("a\xE9\x80","UTF-8","ISO-8859-1");   // aé<control>

I can live with the limitation (detect as ISO, convert as Windows), but I'd love to know why this is the case.

0 Answers
Related