以下代码未检测到编码正确。
$data = 'ABCDEG АБВГДЕ';
$charset = mb_detect_encoding($data);
$data = iconv($charset, "UTF-8", $data);
$data = mb_strtolower($data, 'UTF-8');
$datasort = str_replace(array("\r", "\n", " "), '', $data);
$counter = mb_strlen($datasort,'UTF-8');
foreach (count_chars($datasort, 1) as $i => $val)
{
echo '
<tr>
<th scope="row">'.mb_detect_encoding(chr($i)).'</th>
// ON LATIN SYMBOLS IT DETECTED ANCII AND ON CYRILLIC IT DETECTED **NOTHING**
</tr>
';
}
这可能是问题所在? // php文件有UTF-8编码
答案 0 :(得分:1)
试试这个以及所有ASCII或UTF-8:代码的问题是 count_chars会在转换过程中更改编码...
function mbStringToArray ($string) {
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr($string,0,1,"UTF-8");
$string = mb_substr($string,1,$strlen,"UTF-8");
$strlen = mb_strlen($string);
}
return $array;
}
$data = 'ABCDEG АБВГДЕ';
$data = str_replace(array("\r", "\n", " "), '', mb_strtolower($data));
iconv(mb_detect_encoding($data, mb_detect_order(), true), "UTF-8", $data);
$data = mbStringToArray($data);
echo '<table>';
foreach ($data as $i => $val)
{
echo '
<tr>
<th scope="row">'. $val . ' : ' .mb_detect_encoding(chr($i)).'</th>
</tr>
';
}
echo '</table>';
答:ASCII B:UTF-8 C:UTF-8 D:UTF-8 E:UTF-8 G:UTF-8 А:UTF-8 Б:UTF-8 В:UTF-8 Г:ASCII Ä:ASCII Е:UTF-8