drwurqczo39355510 2014-04-09 13:01
浏览 40
已采纳

文件中的file_get_contents包含西里尔字符和未定义的编码

I cannot get cyrillic characters in php from a .txt file with unknown encoding. I tried almost everything I could find on the web. What php function do I need to use get the contents of this file?

https://www.dropbox.com/s/w7cex4wiogyytvm/100004-6.txt

EDIT

Input:

    $path = WWW_ROOT . 'files' . DS . '100002-6.txt';
    $string = file_get_contents($path);
    debug($string);

Output: debug is broken, if I try to save the value to database it fails (BOM does some trouble and the value cannot be saved).

Input

    $path = WWW_ROOT . 'files' . DS . '100002-6.txt';
    $string = file_get_contents($path);
    $string = mb_convert_encoding ($string , 'utf-8');
    debug($string);

Output:

    '????? ???:300/500V
    ???? ???:2000V
    ????? ???? ??????: ? +70??
    ?? ??? ?? (????? 5 ??.): ? +160??
    ????? ?????? ?? ?????: ? +5??   '

Input:

    $path = WWW_ROOT . 'files' . DS . '100002-6.txt';
    $string = file_get_contents($path);
    $string = iconv("UTF-16", "UTF-8//TRANSLIT//IGNORE", $string);
    debug($string);

Output:

췮㌰〯㔰ざഊ죱㈰〰嘍્⃰⃲㨠‫㜰냑ഊ쿰⃱밠⣭㔠⤺⃤⬱㘰냑ഊ췠볭

Input:

    $path = WWW_ROOT . 'files' . DS . '100002-6.txt';
    $string = file_get_contents($path);
    $string = iconv("ISO-8859-5", "UTF-8//TRANSLIT//IGNORE", $string);
    debug($string);

Output:

    Эюьшэрыхэ эряюэ:300/500V
    Шёяшђхэ эряюэ:2000V
    ЭрМтшёюър №рсюђэр ђхьях№рђѓ№р: фю +70Аб
    Я№ш ъ№рђюъ ёяюМ (эрМьэюуѓ 5 ёхъ.): фю +160Аб
    ЭрМэшёър ђхьях№рђѓ№р я№ш шэёђрырішМр: фю +5Аб

Now that I tested multiple files, I don't think the input file is Unicode encoded anymore. I succeeded on reading my test file, but on the one that matters (and I don't know the encoding of) still nothing. So I changed the question, the encoding seems to be undefined still.

A little bit more for clearance. I can open this file and see it normally in notepad. It contains cyrillic characters that make this problem.

  • 写回答

1条回答 默认 最新

  • douhan6738 2014-04-09 14:47
    关注

    The file is encoded in CP1251 a.k.a. MS-CYRL a.k.a. "Cyrillic (Windows)".

    $string = file_get_contents($path);
    $string = iconv('CP1251', 'UTF-8', $string);
    

    How did I figure this out? Opened the file in a text editor and tried a few relevant encodings until it looked right. There's hardly anything else you can do if the file encoding is unknown.

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

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题