doulianxi0587 2017-10-22 12:32
浏览 31
已采纳

西里尔符号的问题

The following code doesn't detect encoding correct.

$data = 'ABCDEG АБВГДЕ';
$charset = mb_detect_encoding($data);
$data = iconv($charset, "UTF-8", $data);
$data = mb_strtolower($data, 'UTF-8');
$datasort = str_replace(array("", "
", " "), '', $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>
';
}

Where here could be the problem? //php file have UTF-8 encoding

  • 写回答

1条回答 默认 最新

  • dqiatvbi61502 2017-10-22 20:01
    关注

    Try this and all that will be ASCII Or UTF-8 : the problem with your code is that count_chars change the encoding during the convert...

    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("", "
    ", " "), '', 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题
  • ¥15 有没有人能解决下这个问题吗,本人不会编程
  • ¥15 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗