Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP exec change encoding

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?
like image 697
The Bndr Avatar asked Dec 19 '12 21:12

The Bndr


People also ask

How to set UTF-8 encoding in PHP?

PHP UTF-8 Encoding – modifications to your php. The first thing you need to do is to modify your php. ini file to use UTF-8 as the default character set: default_charset = "utf-8"; (Note: You can subsequently use phpinfo() to verify that this has been set properly.)

What is exec() in PHP?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly.

What is UTF-8 PHP?

Definition and Usage. The utf8_encode() function encodes an ISO-8859-1 string to UTF-8. Unicode is a universal standard, and has been developed to describe all possible characters of all languages plus a lot of symbols with one unique number for each character/symbol.

How to use exec command in PHP?

The command that will be executed. If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as \n , is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array.


3 Answers

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.

like image 99
The Bndr Avatar answered Sep 20 '22 05:09

The Bndr


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;
?>
like image 34
Basj Avatar answered Sep 24 '22 05:09

Basj


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);
like image 39
MrD Avatar answered Sep 21 '22 05:09

MrD