duanhe6799 2012-12-15 07:39
浏览 79
已采纳

utf8表示为普通文本

$text = "\xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0";
$text = iconv('UTF-8', 'UTF-8//IGNORE', $text);
var_dump($text); //Тайна - good
$text = file_get_contents('log.txt');
$text = iconv('UTF-8', 'UTF-8//IGNORE', trim($text));
var_dump($text); // \xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0 - bad

Why if string \xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0 was read from file iconv did not work and how to fix it ?

  • 写回答

1条回答 默认 最新

  • donglu8549 2012-12-15 08:05
    关注

    The string literal and the text in the file is not equivalent. $text is already utf-8 (Тайна) and iconv does nothing to it. This is because you use escape sequences to put the actual binary value in the string. with the data in the file \xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0 is not escaped because it was read from a file and stored in a variable so its not a string literal. Try this to convert the data

    $text = file_get_contents('log.txt');
    $text = str_replace('\x', '', trim($text));
    $text = pack('H*', $text);
    var_dump($text); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?