utf8 representation as normal text

我是研究僧i 提交于 2019-12-20 03:23:53

问题


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


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); 


来源:https://stackoverflow.com/questions/13890370/utf8-representation-as-normal-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!