Bug #68119 [Nab]: mb_detect_encoding can not detect encoding rightly

From: Date: Tue, 30 Sep 2014 02:41:54 +0000
Subject: Bug #68119 [Nab]: mb_detect_encoding can not detect encoding rightly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-187765@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68119&edit=1 ID: 68119 Updated by: requinix@php.net Reported by: zf at ancientrock dot org Summary: mb_detect_encoding can not detect encoding rightly Status: Not a bug Type: Bug Package: mbstring related Operating System: Linux PHP Version: master-Git-2014-09-30 (Git) Block user comment: N Private report: N New Comment: I tried responding to your email but your mail server is rejecting my reply. This not a bug. PHP strings do not have encodings - they are just plain bytes. That's why one PHP string can "be" multiple character sequences at once: it depends which encoding you (or your browser) use to interpret those bytes. To your issue, you do not have to detect character encodings yourself because you can tell the browser which encoding to use and it will do so, both when interpreting your HTML and when POSTing form data. Character encodings are not a trivial issue but this is not the place to explain what you need to do. However there are many places on the internet that do explain it so you should search around, or else ask on an online forum or other support channel. http://php.net/support.php Previous Comments: ------------------------------------------------------------------------ [2014-09-30 02:23:29] zf at ancientrock dot org It's really a bug, One word can only had one encoding, can not be all encoding the same time. or we can not coding right ------------------------------------------------------------------------ [2014-09-30 02:11:44] requinix@php.net >EUC-CN: 媼促 Probably not right, actually, taking a second look at my source. But D5BCC2A5 is still a valid byte sequence for that encoding. ------------------------------------------------------------------------ [2014-09-30 02:07:11] requinix@php.net I get E58DA0E6A5BC as invalid EUC-CN. http://3v4l.org/rEcG5 But the rest is correct. Just because it's not the string you expect doesn't mean it's invalid. E58DA0E6A5BC as CP936: 鍗玳ゼ EUC-CN: invalid UTF-8: 占楼 D5BCC2A5 as CP936: 占楼 EUC-CN: 媼促 UTF-8: ռ¥ ------------------------------------------------------------------------ [2014-09-30 00:55:13] zf at ancientrock dot org Description: ------------ mb_detect_encoding can not detect encoding rightly $str1 = hex2bin('e58da0e6a5bc'); $str2 = hex2bin('d5bcc2a5'); we want to detect encoding to see what's it! CP936? EUC-CN ? UTF-8? Test script: --------------- $str1 = hex2bin('e58da0e6a5bc'); $str2 = hex2bin('d5bcc2a5'); var_dump(mb_detect_encoding($str1, 'CP936', true)); var_dump(mb_detect_encoding($str1, 'EUC-CN', true)); var_dump(mb_detect_encoding($str1, 'UTF-8', true)); var_dump(mb_detect_encoding($str2, 'CP936', true)); var_dump(mb_detect_encoding($str2, 'EUC-CN', true)); var_dump(mb_detect_encoding($str2, 'UTF-8', true)); Expected result: ---------------- $str1 was UTF-8 encoding, $str2 was CP936 encoding. Actual result: -------------- All detect return true ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=68119&edit=1

« previous php.bugs (#187765) next »