20

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?
Community
  • 1
  • 1
The Bndr
  • 13,204
  • 16
  • 68
  • 107
  • Wait, are you trying to change the locale of the process spawned by the `exec`, or are you trying to get the `exec` to change *PHP's* locale? – Charles Dec 20 '12 at 04:22
  • @Charles i'm not sure, what exactly you mean. But i found out the following solution (take a look below). – The Bndr Dec 20 '12 at 10:16
  • How did you found out what that your exec command returns `ANSI_X3.4-1968` – Black Nov 17 '15 at 12:08

3 Answers3

42

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.

The Bndr
  • 13,204
  • 16
  • 68
  • 107
  • 2
    finding this makes me one happy man right now. thanks for the q&a – mrP Oct 16 '14 at 21:19
  • 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. – mr_lou Nov 14 '14 at 08:25
  • 1
    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. – j_schultz Apr 21 '15 at 21:15
  • 1
    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! – Suppen May 20 '15 at 06:57
  • Did not worked for me, all my umlauts are still getting replaced by a > – Black Nov 17 '15 at 12:14
  • 1
    putenv('LC_ALL=cs_CZ.utf-8') did the trick for me. suddenly my command with czech characters is not corrupted – Jimmmy Jan 11 '17 at 10:03
  • @EdwardBlack unfortunately 'Did not worked' is not a valid error description. Maybe you should describe your problem in an separate question. – The Bndr Jan 24 '17 at 14:34
5

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;
?>
Basj
  • 41,386
  • 99
  • 383
  • 673
1

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);
MrD
  • 2,423
  • 3
  • 33
  • 57