utf8表示形式为普通文本 [英] utf8 representation as normal text
问题描述
$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
为什么从文件iconv
中读取字符串\xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0
无效,如何解决?
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 ?
推荐答案
字符串文字和文件中的文本不相等. $text
已经是utf-8(Тайна),iconv对此没有任何作用.这是因为您使用转义序列将实际的二进制值放入细绳.文件\xd0\xa2\xd0\xb0\xd0\xb9\xd0\xbd\xd0\xb0
中的数据不会被转义,因为它是从文件中读取并存储在变量中的,因此它不是字符串文字.
试试这个转换数据
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);
这篇关于utf8表示形式为普通文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!