dongwo2772 2012-05-30 02:46
浏览 55
已采纳

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

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部