Unicode for PHP exec

I have a Python file that I call using the PHP exec function. Python then prints a string (apparently Unicode based on the use of isinstance ) that PHP repeats. The problem I am facing is that if my string contains any special characters (like a power character), it will not be output. I'm sure I need to do something to tinker with the encoding, but I'm not quite sure what to do and why.

EDIT: To find out how I call exec , see the following code snippet:

 $tables = shell_exec('/s/python-2.6.2/bin/python2.6 getWikitables.py '.$title); 

Python correctly displays the string when I call getWikitables.py .

EDIT: It definitely looks like something either at the end of Python, or when passing results. When I run strlen on the return values ​​in PHP, I get 0. Can exec only accept a particular type of encoding?

+5
source share
3 answers

On php, you can use methods like utf8_encode() or utf8_decode() to solve your problem.

-1
source

Try setting the LANG environment variable just before running the Python script for http://php.net/shell-exec#85095 :

 shell_exec(sprintf( 'LANG=en_US.utf-8; /s/python-2.6.2/bin/python2.6 getWikitables.py %s', escapeshellarg($title) )); 

(using sprintf() to (hopefully) make it a little easier to follow a long line)

You may also need to do this before calling shell_exec() , for http://php.net/shell-exec#78279 :

 $locale = 'en_US.utf-8'; setlocale(LC_ALL, $locale); putenv('LC_ALL='.$locale); 
+8
source

I had a similar problem and solved it as follows. I don’t understand why this is necessary, because I have already processed everything with UTF-8. Calling my Python script from the command line worked, but not with exec (shell_exec) via PHP and Apache.

According to the php forum entry , this one is needed if you want to use escapeshellarg() :

 setlocale(LC_CTYPE, "en_US.UTF-8"); 

It must be called before executing escapeshellarg() . In addition, it was necessary to set a specific Python environment variable before the command exe (found an unrelated hint here ):

 putenv("PYTHONIOENCODING=utf-8"); 

My Python script evaluated the arguments like this:

 sys.argv[1].decode("utf-8") 

(Hint: this was necessary because I use the library to convert some Arabic texts.)

So, finally, I could imagine that the initial question could be resolved as follows:

 setlocale(LC_CTYPE, "en_US.UTF-8"); putenv("PYTHONIOENCODING=utf-8"); $tables = shell_exec('/s/python-2.6.2/bin/python2.6 getWikitables.py ' . escapeshellarg($title)); 

But I can not say anything about the return value. In my case, I could easily output it to the browser.

Spent many, many hours to figure it out ... One of the situations when I hate my job ;-)

0
source

All Articles