Unicode 到 PHP 执行

发布于 2024-11-08 13:00:47 字数 558 浏览 0 评论 0原文

我有一个使用 PHP 的 exec 函数调用的 Python 文件。然后,Python 输出一个字符串(显然是 Unicode,基于使用 isinstance),PHP 会回显该字符串。我遇到的问题是,如果我的字符串中有任何特殊字符(如度数符号),它就不会输出。我确信我需要做一些事情来摆弄编码,但我不太确定该做什么以及为什么。

编辑:要了解我如何调用exec,请参阅以下代码片段:

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

当我调用getWikitables.py 本身。

编辑:这肯定是Python端的问题,或者是传输结果的问题。当我在 PHP 中对返回值运行 strlen 时,得到 0。 exec 是否只能接受某种类型的编码?

I have a Python file I'm calling with PHP's exec function. Python then outputs a string (apparently Unicode, based on using isinstance), which is echoed by PHP. The problem I'm running into is that if my string has any special characters in it (like the degree symbol), it won't output. I'm sure I need to do something to fiddle with the encoding, but I'm not really sure what to do, and why.

EDIT: To get an idea of how I am calling exec, please see the following code snippet:

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

Python properly outputs the string when I call getWikitables.py by itself.

EDIT: It definitely seems to be something either on the Python end, or in transmitting the results. When I run strlen on the returned values in PHP, I get 0. Can exec only accept a certain type of encoding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冬天旳寂寞 2024-11-15 13:00:47

尝试在按 http://php 执行 Python 脚本之前立即设置 LANG 环境变量.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)
));

使用sprintf() (希望)让它变得更容易一点跟随冗长的字符串)

您可能还/相反需要在调用 shell_exec() 之前执行此操作,按照 http://php.net/shell-exec#78279

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

Try setting the LANG environment variable immediately before executing the Python script per 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)
));

(use of sprintf() to (hopefully) make it a little easier to follow the lengthy string)

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

$locale = 'en_US.utf-8';
setlocale(LC_ALL, $locale);
putenv('LC_ALL='.$locale);
木槿暧夏七纪年 2024-11-15 13:00:47

我遇到了类似的问题并通过以下方法解决了它。我不明白为什么有必要,因为我虽然所有内容都已经用 UTF-8 处理过。在命令行上调用我的 Python 脚本可以工作,但不能通过 PHP 和 Apache 调用 exec (shell_exec)。

根据 php 论坛条目,当您想要使用 escapeshellarg()

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

它需要是在执行escapeshellarg() 之前调用。此外,还需要设置某个 Python 环境变量在 exec 命令之前(在此处找到了一个不相关的提示):

putenv("PYTHONIOENCODING=utf-8");

我的Python脚本评估了这样的参数:(

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

提示:那是必需的,因为我使用 library 来转换一些阿拉伯语文本。)

所以最后,我可以想象一下原来的问题可以这样解决:

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));

但我无法说出有关返回值的任何信息。就我而言,我可以将其直接输出到浏览器,没有任何问题。

花了很多很多时间才找到答案......这是我讨厌我的工作的情况之一;-)

I have had a similar issue and solved it with the following. I don't understand why it is necessary, since I though all is already processed with UTF-8. Calling my Python script on the command line worked, but not with exec (shell_exec) via PHP and Apache.

According to a php forum entry this one is needed when you want to use escapeshellarg():

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

It needs to be called before escapeshellarg() is executed. Also, it was necessary to set a certain Python environment variable before the exec command (found an unrelated hint here):

putenv("PYTHONIOENCODING=utf-8");

My Python script evaluated the arguments like this:

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

(Hint: That was required because I use a library to convert some arabic texts.)

So finally, I could imagine that the original question could be solved this way:

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 cannot tell anything regarding the return value. In my case I could output it to the browser directly without any problems.

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

み零 2024-11-15 13:00:47

这对我有用

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));

This worked for me

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));
情绪 2024-11-15 13:00:47

在 php 上,您可以使用类似 utf8_encode() 的方法或 utf8_decode() 来解决您的问题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文