PHP exec change encoding

15,949

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);
Share:
15,949
The Bndr
Author by

The Bndr

Updated on June 06, 2022

Comments

  • The Bndr
    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:

    1. Why is there an different result be executing the php command at bash and at apache_module/web page?
    2. How to set UTF-8 for exec if it runs inside a website as apache module?
  • mrP
    mrP over 9 years
    finding this makes me one happy man right now. thanks for the q&a
  • mr_lou
    mr_lou over 9 years
    This 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
    j_schultz about 9 years
    Note 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
    Suppen almost 9 years
    Thank 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
    Black over 8 years
    Did not worked for me, all my umlauts are still getting replaced by a <?>
  • Jimmmy
    Jimmmy over 7 years
    putenv('LC_ALL=cs_CZ.utf-8') did the trick for me. suddenly my command with czech characters is not corrupted
  • The Bndr
    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
    jerik over 4 years
    Solved exactly my problem. Thumbs up for the documentation!
  • phili_b
    phili_b over 4 years
    I got invalid input found on input connection in R language source()called by php. php-> shell ->R.You save me :)