PHP函数iconv(),mb_convert_encoding()无法将KOI8-R转换为CP1251

时间:2019-10-11 10:29:34

标签: php iconv

使用Windows应用程序将数据从Cronos数据库导出到TXT文件后,我遇到了问题-无法在Linux服务器上正确查看导出的数据(在Windows上可以查看)。

这里是一个例子:

let matchingClient = null; for (let i = 0; i < windowClients.length; i++) { const windowClient = windowClients[i]; if (windowClient.url === urlToOpen) { matchingClient = windowClient; break; } } if (matchingClient) { // if window is already open then just focus on window matchingClient.focus(); // post the message channel.postMessage({action: event.action,information:event.notification.data ? event.notification.data.info : ""}); } else { //if window is not open yet, then open a new window clients.openWindow(urlToOpen); //have used setTimeout because opening a window may take time , however this is just a workaround setTimeout(function(){ console.log("Delayed"); //post the message channel.postMessage({action: event.action,information:event.notification.data ? event.notification.data.info : ""}); },4000); } });

我尝试使用PHP进行转换,因为以后需要将其转换为巨大的SQL文件。

字符集解码服务向我展示了此文本应从KOI8-R转换为CP1251,因此我已经使用iconv()和mb_convert_encoding()进行了尝试

因此,让我们尝试一下:

цНПНД|||пЮИНМ|||сКХЖЮ

结果是: $string = iconv('KOI8-R', 'CP1251', $string);

我已经在// IGNORE上进行了搜索,然后再试一次: Notice: iconv(): Detected an illegal character in input string

结果是: 相同的字符串- $string = iconv('KOI8-R', 'CP1251//IGNORE', $string);

然后是mb_convert_encoding,没有“ from-encoding”参数: $ string = mb_convert_encoding($ string,'CP1251');

结果是: цНПНД|||пЮИНМ|||сКХЖЮ

...,并带有“来自编码”: ?????|||?????|||?????

然后进行镜像尝试(cp1251至koi8-r)。使用iconv(),我得到了“在输入字符串中检测到非法字符”; mb_convert_encoding给了我一些新的东西: цНПНД|||пЮИНМ|||сКХЖЮ 尝试将其转换为UTF-8会显示很多不同的符号。

我也想使用Notepad ++转换此文件,但是它不允许我打开2.5GB的txt文件:(

我也曾尝试将iconv用作二进制文件-同样的错误。

我需要的一切都是将其转换为可读的俄语符号。最好的方法是将其转换为UTF-8,但我不怎么做

зПТПД|||тБКПО|||хМЙГБ Linux node03 4.4.0-142-generic #168-Ubuntu SMP Wed Jan 16 21:00:45 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

iconv (Ubuntu GLIBC 2.23-0ubuntu11) 2.23

2 个答案:

答案 0 :(得分:0)

将CP1252与西里尔字母一起使用不是一个好主意。使用UTF-8。

PHP还可以在内部处理带有字符集!= UTF-8的字符串。 尝试在具有UTF-8标头的页面上输出这些字符串将导致错误的输出。 十六进制表示法可以在UTF-8页面上显示此类字符串,也可以在UTF-8下的编辑器中进行编写。 函数strToHex2()提供了这样的字符串。

//Returns a string with escaped chars in hexadecimal notation
function strToHex2($str) {
   return '\x'.rtrim (chunk_split(strtoupper(bin2hex($str)), 2,'\x'),'\x'); 
}

以下几行显示Iconv执行了从KOI8-R到UTF8的转换(反之亦然):

$cityUTF8 = "Город|||";

//convert to KOI8-R
$cityKOI8 = iconv('UTF-8','KOI8-R',$cityUTF8);
echo "KOI8: ".strToHex2($cityKOI8)."<br>";
//KOI8: \xE7\xCF\xD2\xCF\xC4\x7C\x7C\x7C
//compare with https://en.wikipedia.org/wiki/KOI8-R

//convert back to UTF-8 from KOI8 for a check
$cityUTF8Back = iconv('KOI8-R','UTF-8',$cityKOI8); 

//check if equal
var_dump($cityUTF8 === $cityUTF8Back);  //bool(true)

答案 1 :(得分:0)

源编码不是KOI8-R。是CP1251

我使用iconv将文件从CP1251转换为UTF-8,这很有帮助。