I need to address UTF-8 filenames with the php exec command. The problem is that the php exec
command does not seem to understand utf-8. I use something like th
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.
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);
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;
?>