John Davidson

PHP: How to configure windows shell codepage so that STDOUT of proc_open() does not garble?

0 comments
Message:


The following PHP code


$descriptorspec = array(1=>array('pipe', 'w'));
$cmd = escapeshellcmd('echo こんにちは');

// Change this line in the following snippets
$proc = proc_open($cmd, $descriptorspec, $pipes);

$res = null;

if (is_resource($proc))
{
$res = stream_get_contents($pipes[1]);
proc_close($proc);
}

echo $res;

outputs the gibberish $res = ����ɂ���.


I tried this solution by setting up $env_vars in proc_open(). That is, I substituted the third line of the above snippet with


$encoding = array('LANG'=>'ja_JP.utf-8');
$proc = proc_open($cmd, $descriptorspec, $pipes, null, $encoding);

Still outputs the garbled string $res = ����ɂ���.


Next, I tried to use setlocale() and putenv() as per this solution. The third line of the first snippet becomes


$encoding = 'ja_JP.UTF-8';
setlocale(LC_ALL, $encoding);
putenv('LC_ALL='. $encoding);

$proc = proc_open($cmd, $descriptorspec, $pipes);

Still outputs the gibberish $res = ����ɂ���...


Do you know what's wrong with the shell encoding config?


As a side note, I'm currently debugging my code on Visual Studio 2022 with IIS Express (Win10/11), but will eventually deploy my website on an Apache server.


Additional info:



  • I use IIS Express launched from Visual Studio 2022 and an external WAMPServer as debugging servers. Both output garbled results.

  • The version of PHP is 8.1.

  • The OS is Windows 11 Enterprise.

  • My PHP file is correctly saved in UTF-8 (without BOM)

  • Important note 1: the original code works in the PHP interactive shell opened from PowerShell 7.2 64bits.

  • Important note 2: the original code works on another computer using Windows 10 Home.



My Answer:
It seems like the issue might be related to the encoding settings of the Windows shell. Here are a few things you can try to configure the Windows shell codepage:

1. Change the codepage of the Windows shell before running the PHP script. You can do this by running the following command in the command prompt:


chcp 65001


This command sets the codepage to UTF-8, which should help with displaying non-ASCII characters correctly.

2. Make sure that the font used in the command prompt supports the characters you are trying to display. You can change the font by right-clicking on the title bar of the command prompt, selecting "Properties," and then going to the "Font" tab to choose a font that supports the characters.

3. If you are running the PHP script in a web server environment (such as Apache), you can try setting the default_charset directive in your php.ini file to UTF-8:


default_charset = "UTF-8"


This will ensure that PHP outputs data in UTF-8 encoding by default.

4. If you are still experiencing issues, you can try explicitly setting the encoding of the output stream in your PHP script using the mb_convert_encoding function:

php
$res = mb_convert_encoding(stream_get_contents($pipes[1]), 'UTF-8', 'auto');


This will convert the output from the command to UTF-8 encoding before displaying it.

By trying these steps, you should be able to configure the Windows shell codepage to display non-ASCII characters correctly in the STDOUT of the proc_open() function.

Rate this post

3 of 5 based on 5386 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us