问题
$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