utf8表示形式为普通文本 [英] utf8 representation as normal text

查看:87
本文介绍了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

为什么从文件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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆