PHP - MySQL 到 SimpleXML,确保正确的编码
好的,我正在为一些客户开发一些插件,这些插件基本上从他们的 MySQL 数据库中获取数据,然后通过以下方式发送给我们。使用 SimpleXML 生成的 XML。
现在,当我们收到 XML 文件时,我们通过脚本运行它,将数据放入数据库中,问题就来了。
当我们将客户数据放入数据库时,在某些情况下,某些字符会被转换为中文字母。 (我们使用 UTF-8)
现在我想如果我有办法确定客户端数据库的编码,然后转换为 UTF8 并为 XML 文件提供标头 。我的问题是检测客户端数据库给我的编码,并将其正确转换为 UTF-8。
我查看了 php 的 mb_detect_encoding()
和 mb_convert_encoding
,但不确定“多字节字符串”扩展有多常见,我想保持兼容性插件的值尽可能高。
关于我如何做到最好有什么想法吗?如果您需要更多信息,请告诉我。
编辑:好的,使用mysql_set_charset('UTF8')
并将SimpleXML设置为
完成了这项工作,感谢您的帮助。
接受戴兹的回答,因为他是引导我找到这一解决方案的人。
Okay, so i'm working on some plugins for some of our clients, that basically takes data from their MySQL database, and sends it to us via. XML generated with SimpleXML.
Now, when we receive the XML-file we run it through a script putting their data into our database, and here comes the problem.
When we put the clients data into our database, in some cases some of the characters are converted to chinese letters. (We use UTF-8)
Now i figure this could be resolved if i had a way to determine the encoding of the clients database, and convert to UTF8 and give the XML-file the header <?xml version="1.0" encoding="UTF-8" ?>
. My problem is to detect the encoding given to me from the clients database, and converting it to UTF-8 properly.
I've had a look at php's mb_detect_encoding()
and mb_convert_encoding
, but are unsure of how common the "Multibyte String" extension is, and i would like to keep the compatibility of the plugin as high as possible.
Any ideas as to how i do this best? Let me know if you need more information.
EDIT: Okay, use of mysql_set_charset('UTF8')
and setting SimpleXML to <?xml version="1.0" encoding="UTF-8" ?><xml/>
does the job, thanks for the help.
Accepting daids answer since he was the one to lead me to this solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取MySQL返回的字符集:
mqsoh的答案(http://stackoverflow.com/questions/7880492/php-mysql-to-simplexml-ensure-proper-encoding/7880779#7880779)将转换数据。
To get what charset MySQL return :
mqsoh's answer (http://stackoverflow.com/questions/7880492/php-mysql-to-simplexml-ensure-proper-encoding/7880779#7880779) will convert data.
这个问题建议使用iconv,这是 PHP 默认启用的扩展。
This question suggested the use of iconv, which is an extension enabled by default in PHP.