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 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C