PHP UTF-8 to Windows command line encoding

前端 未结 4 1188
广开言路
广开言路 2020-12-09 04:50

Everything is in the question : I have a Php script that is a UTF-8 file. In this script I want to do this :

  
相关标签:
4条回答
  • 2020-12-09 05:32

    It looks the default encoding is Code page 437.

    0 讨论(0)
  • 2020-12-09 05:34

    You put me on the right track but there was kinddof a problem (I love Windows \o/) :

    C:\php>chcp 65001
    Page de codes active : 65001
    C:\php>php -c C:\WINDOWS\php.ini -f mysqldump.php | more
    Mémoire insuffisante.
    

    Mémoire insuffisante = not enough memory.

    If I try

    C:\php>chcp 1252
    C:\php>php -c C:\WINDOWS\php.ini -f mysqldump.php
    C:\php>ééîîïïÂÂÂÂâûü
    

    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 :

      echo mb_convert_encoding($utf8_string, "pass", "auto");
    
    0 讨论(0)
  • 2020-12-09 05:35

    The problem is Windows cmd line by default does not support UTF8. From this link, if you follow these

    1. Open a command prompt window
    2. Change the properties of the window to use something besides the default raster font. he Lucida Console True Type font seems to work well.
    3. Run "chcp 65001" from the command prompt

    You should be able to output utf8.

    0 讨论(0)
  • 2020-12-09 05:56

    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;
        }
    }
    
    0 讨论(0)
提交回复
热议问题