PHP exec change encoding
This solves it for me (source: this comment here):
<?php
putenv('LANG=en_US.UTF-8');
$command = escapeshellcmd('python3 myscript.py');
$output = shell_exec($command);
echo $output;
?>
I had the similar problem. My program was returning me some German letters like: üäöß. Here is my code:
$programResult = shell_exec('my script');
Variable $programResult
is containing German umlauts, but they were badly encoded. In order to encode it properly you can call utf8_encode()
function.
$programResult = shell_exec('my script');
$programResult = utf8_encode($programResult);
To answer my own question - i found the following solution:
setting the locale environment variable with PHP
$locale='de_DE.UTF-8';
setlocale(LC_ALL,$locale);
putenv('LC_ALL='.$locale);
echo exec('locale charmap');
This sets to / returns UTF-8
. So i'm able to pass special characters and umlauts to linux shell commands.