为何mbstring函数无法正确识别ISO-8859字符串?
作者:互联网
尽管listing each ISO-8859
character set as an individual encoding,mbstring函数可互换地对待每个ISO-8859字符集.开车回家:
$strings = [
'English' => 'Ea vim decore sapientem repudiandae. Sea cu delenit gamu mutn, tic.',
'Cyrillic' => 'Лорем ипсум долор сит амет, ин ехерци вереар номинати яуи, сит ин омниум инермис но.',
'Greek' => 'Λορεμ ιπσθμ δολορ σιτ αμετ, ηασ γραεcο νθσqθαμ cθ, εστ θτ εσσε διcαμ qθαλισqθε cθ.',
'Armenian' => 'լոռեմ իպսում դոլոռ սիթ ամեթ, եամ նո թաթիոն ծոմպռեհենսամ, իուս ադ նիսլ ոմնիս մինիմ եսթ',
'Georgian' => 'ლორემ იფსუმ დოლორ სით ამეთ, ეხ ყუანდო ცოფიოსაე უსუ, იუს ეუ ჰინც ვერო დომინგ ჰის',
'Hindi' => 'वर्ष एसेएवं व्याख्यान संदेश होने लक्षण एसेएवं पहोचाना विचरविमर्श? वर्णन करती आशाआपस अन्तरराष्ट्रीयकरन. रहारुप कार्यसिधान्त',
'Korean' => '모든 국민은 보건에 관하여 국가의 보호를 받는다, 전직대통령의 신분과 예우에 관하여는 법',
'Arabic' => 'مع لهذه الهجوم عدم, فكان اتفاق الصفحات من أسر. وجزر عُقر أما بـ, عل دار بقسوة المتّبعة بالولايات. وإقامة والفرنسي كل لكل. أي',
'Hebrew' => 'עמוד מדינות, חפש ואלקטרוניקה אנתרופולוגיה דת, מה קהילה הקהילה טכנו'
];
$encodings = ['ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15' ];
foreach( $strings as $lang => $text ) {
echo $lang . " is encoded as " . mb_detect_encoding( $text, $encodings ) . "\n";
foreach( $encodings as $encoding ) {
echo " - is " . (mb_check_encoding( $text, $encoding ) ? "" : "not ") . $encoding . "\n";
}
}
这产生了以下效果的输出
Hindi is encoded as ISO-8859-1
- is ISO-8859-1
- is ISO-8859-2
- is ISO-8859-3
- is ISO-8859-4
- is ISO-8859-5
- is ISO-8859-6
- is ISO-8859-7
- is ISO-8859-8
- is ISO-8859-9
- is ISO-8859-10
- is ISO-8859-13
- is ISO-8859-14
- is ISO-8859-15
with identical results for every listed language,这显然是不正确的.
为什么mbstring会分别列出每种ISO-8859编码,但要互换使用呢?有什么方法可以可靠地检测出正确的规格?
还是我只是滥用这些功能?
解决方法:
mb_detect_encoding会猜测编码可能是什么,这种事情是不可能准确的(并且该函数没有做太多尝试.)
mb_check_encoding告诉您字符串是否由对给定编码有效的字节序列组成,并且鉴于每个可能的字节在每个ISO-8859- *中均有效,因此针对它们进行验证毫无意义(它们将始终返回true.)
对于相关阅读,我非常建议:The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets
标签:iso-8859-1,character-encoding,mbstring,php 来源: https://codeday.me/bug/20191111/2020713.html