PHP UTF-8 转 Windows 命令行编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1650369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP UTF-8 to Windows command line encoding
提问by Olivier Pons
Everything is in the question : I have a Php script that is a UTF-8 file. In this script I want to do this :
一切都在问题中:我有一个 UTF-8 文件的 PHP 脚本。在这个脚本中,我想这样做:
<?
echo "aê??\n";
?>
If I run it in a Windows prompt I get this :
如果我在 Windows 提示符下运行它,我会得到这个:
C:\php>php -c C:\WINDOWS\php.ini -f mysqldump.php
├ó├?├?├╗
C:\php>
I've not been able to find the right conversion scheme. I've tried also this code :
我一直无法找到正确的转换方案。我也试过这个代码:
$tab = mb_list_encodings();
foreach ($tab as $enc1) {
foreach ($tab as $enc2) {
$t=mb_convert_encoding("aê??\n", $enc1, $enc2);
if (strlen($t)<14) {
echo $enc1." ".$enc2." = ".$t."\n";
}
}
}
And I didn't find the right conversion !
而且我没有找到正确的转换!
Any help would be greatly appreciated
任何帮助将不胜感激
采纳答案by Olivier Pons
You put me on the right track but there was kinddof a problem (I love Windows \o/) :
你让我走上正轨,但有点问题(我喜欢 Windows \o/):
C:\php>chcp 65001
Page de codes active?: 65001
C:\php>php -c C:\WINDOWS\php.ini -f mysqldump.php | more
Mémtheitroade insuffisante.
Mémtheitroade insuffisante = not enough memory.
Mémtheitroade insuffisante = 内存不足。
If I try
如果我尝试
C:\php>chcp 1252
C:\php>php -c C:\WINDOWS\php.ini -f mysqldump.php
C:\php>éé????????a?ü
it works. Only God knows why. But it works. Thanks for putting me on the right track !!
有用。只有上帝知道为什么。但它有效。谢谢你让我走上正轨!!
By the way the php code to go properly form UTF8 to command prompt is :
顺便说一下,php 代码正确地形成 UTF8 到命令提示符是:
echo mb_convert_encoding($utf8_string, "pass", "auto");
回答by Doug T.
The problem is Windows cmd line by default does not support UTF8. From this link, if you follow these
问题是 Windows cmd 行默认不支持 UTF8。从这个链接,如果你遵循这些
- Open a command prompt window
- Change the properties of the window to use something besides the default raster font. he Lucida Console True Type font seems to work well.
- Run "chcp 65001" from the command prompt
- 打开命令提示符窗口
- 更改窗口的属性以使用除默认光栅字体之外的其他内容。Lucida Console True Type 字体似乎运行良好。
- 从命令提示符运行“chcp 65001”
You should be able to output utf8.
你应该能够输出utf8。
回答by user3551026
Try this another one. It's working with Russian encoding, I hope it will work with French:
试试这个另一个。它适用于俄语编码,我希望它适用于法语:
class ConsoleHelper
{
/**
* @var boolean
*/
private static $isEncodingSet = false;
/**
* @param string $message
* @return string
*/
public static function encodeMessage($message)
{
$isWindows = (DIRECTORY_SEPARATOR == '\');
if ($isWindows) {
if ( ! self::$isEncodingSet) {
shell_exec('chcp 866');
self::$isEncodingSet = true;
}
$message = iconv('utf-8', 'cp866', $message);
}
return $message;
}
}
回答by Michas
It looks the default encoding is Code page 437.
看起来默认编码是Code page 437。