Unicode 到 PHP 执行
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试在按 http://php 执行 Python 脚本之前立即设置
LANG
环境变量.net/shell-exec#85095:(使用
sprintf()
(希望)让它变得更容易一点跟随冗长的字符串)您可能还/相反需要在调用
shell_exec()
之前执行此操作,按照 http://php.net/shell-exec#78279:Try setting the
LANG
environment variable immediately before executing the Python script per http://php.net/shell-exec#85095:(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:我遇到了类似的问题并通过以下方法解决了它。我不明白为什么有必要,因为我虽然所有内容都已经用 UTF-8 处理过。在命令行上调用我的 Python 脚本可以工作,但不能通过 PHP 和 Apache 调用 exec (shell_exec)。
根据 php 论坛条目,当您想要使用
escapeshellarg()
:它需要是在执行
escapeshellarg()
之前调用。此外,还需要设置某个 Python 环境变量在 exec 命令之前(在此处找到了一个不相关的提示):我的Python脚本评估了这样的参数:(
提示:那是必需的,因为我使用 library 来转换一些阿拉伯语文本。)
所以最后,我可以想象一下原来的问题可以这样解决:
但我无法说出有关返回值的任何信息。就我而言,我可以将其直接输出到浏览器,没有任何问题。
花了很多很多时间才找到答案......这是我讨厌我的工作的情况之一;-)
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()
: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):My Python script evaluated the arguments like this:
(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:
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 ;-)
这对我有用
This worked for me
在 php 上,您可以使用类似
utf8_encode()
的方法或utf8_decode()
来解决您的问题。On php you can use methods like
utf8_encode()
orutf8_decode()
to solve your problem.