dongwo2772 2012-05-30 10:46
浏览 54
已采纳

PHP utf-8由char读取文件

I have some serious problem dealing with files in php with utf-8 encoding. I need to read a file char by char or possibly line by line, with fixed small chunks. I tried to use fgetc and fgets but characters encoded in utf8 are split in a few "chars" instead of only one. I'm trying to find a solution, it could be even a small "string" containing only the character in Unicode but it must be read from file properly.

Pseudo code I would like to use:

while ( ($c == read_utf8_char($file)) != false ) {
    if ($c == 'ó') do_something();
}

I would appreciate your help.

  • 写回答

1条回答 默认 最新

  • dongqin1819 2012-05-30 10:51
    关注
    function mb_str_split($string) { 
        return preg_split('/(?<!^)(?!$)/u', $string ); 
    } 
    
    $chars = mb_str_split($file);
    
    foreach ($chars as $c) {
        if ($c == 'ó') do_something();
    }
    

    ~ Source: http://www.php.net/manual/en/function.mb-split.php#99851

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

报告相同问题?

悬赏问题

  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了