西里尔符号的问题

Problems with Cyrillic symbols

以下代码未检测到编码正确。

$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 编码

试试这个,所有这些都将是 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>';

A : 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