PHP Ansi到UTF-8 [英] PHP Ansi to UTF-8

查看:286
本文介绍了PHP Ansi到UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在PHP中创建一个脚本,将一些文件转换为UTF-8。我有一个希腊文件,其中Notepad ++表示它是ANSI编码。当我将其上传到服务器时,它会检测到它的编码为UTF-8(我认为的东西)。然后当用UTF8_encode()将其内容转换为UTF-8并下载新文件时,这些字符被弄乱了。我试图用PHP删除BOM,结果是一样的。我尝试使用PHP删除BOM,而不将文件转换为UTF-8,但该文件仍保留在ANSI编码中,而不会弄乱字符。如何解决?

I'm trying to create a script in PHP for converting some files to UTF-8. I have a file in Greek, where Notepad++ indicates that it ahs "ANSI" encoding. When I upload it to the server, it detects it's encoding as UTF-8 (something wrinf i think). Then when I convert it's contents to UTF-8 with utf8_encode () and download the new file, the characters are messed up. I tried to remove the BOM with PHP and the result was the same. I tried to remove the BOM with PHP without converting the file to UTF-8 but the file remained in ANSI encoding, without messed up characters. How can I fix that?

推荐答案

删除BOM,然后执行:

Remove BOM, then do:

$file = file_get_contents('file.php');
$file = iconv('greek-charset','UTF-8', $file);
file_put_contents('file.php', $file);
//ta-da!

更改希腊字符集正确的名称字符集(也许 Windows-1253 )。

Change greek-charset to correct name of charset (maybe Windows-1253).

这篇关于PHP Ansi到UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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