PHP exec change encoding
Solution 1
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.
Solution 2
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;
?>
Solution 3
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);
The Bndr
Updated on June 06, 2022Comments
-
The Bndr almost 2 years
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 this:echo exec('locale charmap');
returns
ANSI_X3.4-1968
looking at this SO question, the solution lookes like that:
echo exec('LANG=de_DE.utf8; locale charmap');
But I still get the same output:
ANSI_X3.4-1968
On the other hand - if I execute this php command on the bash command line:
php -r "echo exec('LANG=de_DE.UTF8 locale charmap');"
The output is UTF-8. So the questions are:
- Why is there an different result be executing the php command at bash and at apache_module/web page?
- How to set UTF-8 for
exec
if it runs inside a website as apache module?
-
mrP over 9 yearsfinding this makes me one happy man right now. thanks for the q&a
-
mr_lou over 9 yearsThis is great. I'd set the charset in a config file in /etc but after a server-upgrade from Ubuntu 12.04 to 14.04, that config was apparently gone. So it's better to set it in the PHP file to avoid future problems it seems.
-
j_schultz about 9 yearsNote that you can also specify
LC_ALL=...
right in front of your exec call (like in the original post with the wrong environment variables) so that it affects only that one call if needed, e.g.exec('LC_ALL=de_DE.UTF-8 locale charmap')
- sometimes, this might look cleaner. -
Suppen almost 9 yearsThank you. This helped me a lot. I don't understand how it is possible for a language which is supposed to be "modern" and is pretty much the most widely used language in the world to default to a charset designed in 1968!
-
Black over 8 yearsDid not worked for me, all my umlauts are still getting replaced by a <?>
-
Jimmmy over 7 yearsputenv('LC_ALL=cs_CZ.utf-8') did the trick for me. suddenly my command with czech characters is not corrupted
-
The Bndr about 7 years@EdwardBlack unfortunately 'Did not worked' is not a valid error description. Maybe you should describe your problem in an separate question.
-
jerik over 4 yearsSolved exactly my problem. Thumbs up for the documentation!
-
phili_b over 4 yearsI got
invalid input found on input connection
in R languagesource()
called by php. php-> shell ->R.You save me :)