How to convert output of windows shell exec in php to utf8

Issue

I’m trying to convert the output of one of the commands in PHP that execute shell to utf8 because json_encode returns an empty string.

I’ve tried to use mb_detect_encoding to detect encoding and I wanted to use mb_convert_encoding but mb_detect_encoding is returning empty string.

I’ve got output like this:

2016-04-29  10:09         2�216�720 0.8.1.zip

I’ve also tried this:

sapi_windows_cp_conv(sapi_windows_cp_get(), 65001, $output);

but this return:

sapi_windows_cp_conv(): Wide char conversion failed

Output code page found in this question PHP UTF-8 to Windows command line encoding

Also tried to convert to utf8 before I’m executing dir using:

shell_exec('chcp 65001 && dir');

but I’ve got the same results, weird � instead of space and empty mb_detect.

How can I convert the output of shell command on windows to UTF8?

Solution

function sapi_windows_cp_get() accept string as argument

$output = sapi_windows_cp_conv(sapi_windows_cp_get('oem'), 65001, $output);

Answered By – jcubic

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published