使用 UTF-8 POST 变量调用 Php 脚本
AS3 文档说 AS3 中的字符串采用 UTF-16 格式。
Flash Clip 上有一个文本框,用户可以在其中键入一些数据。
当单击按钮时,我希望将此数据发送到 php 脚本。
我已完成所有设置,但 PHP 脚本似乎获取 UTF-16 格式的数据。数据库中的数据(UTF-8)显示一些无法识别的字符(其中使用了特殊字符),这意味着数据未以正确的编码发送。
var variables:URLVariables=new URLVariables;
var varSend:URLRequest=new URLRequest("http://website.com/systematic/accept.php");
varSend.method=URLRequestMethod.POST;
varSend.data=variables;
var varLoader:URLLoader=new URLLoader;
varLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
单击提交按钮时,将执行以下处理程序。
function sendData(event:MouseEvent) : void {
// i guess here is the problem (tbName.text is UTF-16)
variables.name = tbName.text;
varLoader.load(varSend);
}
有什么方法可以发送数据以便 PHP 脚本获取 UTF-8 格式的数据吗?
(PHP 脚本使用 $_POST['name'] 检索值)。
编辑(在有用的评论之后):
我尝试使用 iconv() 将 POST 变量转换为 UTF-8,但我只得到问号。这可能意味着我错误地假设 php 获取了 UTF-16 格式的字符串。看来不是。但现在我完全不知道如何解决这个问题。
有问题的字符是克罗地亚语字符(č、ć、š、ž 和 đ),全部采用 Windows-1250 编码。如果我手动编辑 phpMyAdmin 中的条目并输入任何这些字符,一切都会很好。但如果我通过闪存来做到这一点,它就不起作用了。
另一个奇怪的事情是,我删除了 iconv 并将所有内容都像一开始一样,现在只有非特殊字符会显示在第一个克罗地亚语中。例如,如果我写“ačasdfadfa”,则只有“a”存储在数据库中。
另一个编辑:
我现在尝试将克罗地亚语字符切换为 ASCII(请参见下面的代码),然后在 PHP 端将它们返回为 č、ć 等。 Flash 显然会向右转发(因为不再有特殊字符),但 PHP 无法正确地将字符转换回字符串。这就是我正在做的事情。
所以我猜这是 PHP 的问题,或者它如何将数据存储到数据库。 PHP 文件本身是 UTF-8 编码的。
AS3 documentation says that Strings in AS3 are in UTF-16 format.
There is a textbox on a Flash Clip where user can type some data.
When a button is clicked, I want this data to be sent to a php script.
I have everything set up, but it seems that the PHP script gets the data in UTF-16 format. The data in the database (which is utf-8) shows some unrecognizable characters (where special characters are used), meaning that the data has not been sent in a correct encoding.
var variables:URLVariables=new URLVariables;
var varSend:URLRequest=new URLRequest("http://website.com/systematic/accept.php");
varSend.method=URLRequestMethod.POST;
varSend.data=variables;
var varLoader:URLLoader=new URLLoader;
varLoader.dataFormat=URLLoaderDataFormat.VARIABLES;
varLoader.addEventListener(Event.COMPLETE, completeHandler);
When submit button is clicked, the following handler gets executed.
function sendData(event:MouseEvent) : void {
// i guess here is the problem (tbName.text is UTF-16)
variables.name = tbName.text;
varLoader.load(varSend);
}
Is there any way to send the data so that PHP script gets the data in UTF-8 format?
(PHP script is retrieving the value using the $_POST['name']).
EDIT (after useful comments):
I've tried to convert the POST variable to UTF-8 by using iconv() but I get only question marks. That may mean that I was wrong in assuming the php got the string in a UTF-16 format. It seems not. But now I have absolutely no idea how to fix this.
The characters in question are croatian characters (č, ć, š, ž and đ), all are in Windows-1250 encoding. If I manually edit the entry in phpMyAdmin and enter any of those characters, everything works great. But if I am doing it thru flash, it doesn't work.
Another strange thing, I have removed iconv and put everything like it was in the beginning and now only non-special characters get shown up to first croatian. For example, if I write "ačasdfadfa", only the "a" gets stored int the database.
Another edit:
I have now tried to switch the croatian characters into something ASCII (see the code below) and then return them to č, ć, etc. on the PHP side. Flash obviously forwards this right (since there's no special characters anymore), but the PHP does not correctly convert the chars back to the string. This is what I was doing.
so I guess it's something with PHP or how it stores the data into the database. The PHP file itself is UTF-8 encoded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更新:
如果您从 Flash 获取的确实是 UTF-16 数据,您可以作为快速修复尝试在 PHP 中转换输入:
您可以尝试这样做并检查数据是否看起来正常。
但是,如果可能的话,我认为最好让 Flash 发送 UTF-8 数据。
旧答案:
<推测>
对我来说,这听起来不像 UTF-16,而更像是在 ISO-8859-1 编码环境中显示的 UTF-8 数据。你在哪里输出变量?您正在执行输出的页面是否也以 UTF-8 编码? (请参阅浏览器的“编码”菜单)如果我必须打赌,我会说 Flash 正确地将它们传输为 UTF-8,但问题出在
输出端。
Update:
If it's indeed UTF-16 data you are getting from Flash, you could as a quick fix try converting the input in PHP:
you could try that and check whether the data looks okay then.
however, if at all possible, I would say it would be preferable to get Flash to send UTF-8 data.
Old answer:
<speculation>
This doesn't sound like UTF-16 to me but more like UTF-8 data being displayed in an ISO-8859-1 encoded environment. Where are you outputting the variables? Is the page you are doing the output on encoded in UTF-8 as well? (See the browser's "encoding" menu) If I had to bet, I would say Flash is transmitting them correctly as UTF-8, but the problem is on the
output end.
</speculation>
如果出现问号,可能是PHP解析错误,而不是flash。
我把这个 .htaccess 文件(针对 php5)放在我工作的日语网站或带有特殊字符的网站的基本目录中......它解决了 99% 的 PHP 问题,但你仍然需要监视输入字符串(我是我自己还在习惯)
if question marks are appearing, it is probably PHP parsing it wrong, not flash.
I put this .htaccess file (for php5) in the base dir of sites I work on in Japanese, or sites with special characters... it solves 99% of problems with PHP but you still need to monitor input strings (I'm still getting used to this myself)