PHP Sax 解析器和 UTF-8
不幸的是,我在 php sax 解析器和 utf-8 编码方面遇到了一些麻烦。
案例:
我有一个以 utf-8 编码的 xml 文件。该文件使用标准 php sax 解析器进行解析。数据被存储到一些容器对象中并插入到mysql数据库中。不幸的是,有些字符在数据库中看起来很奇怪(主要是德语元音变音)。例如,Gürtel 看起来像 Gürtel。
以下代码片段显示了如何实例化解析器:
$saxParser = xml_parser_create("UTF-8");
这足以解析 utf-8 文件吗?如果是的话,我缺少什么?插入时一些特殊的数据库内容?
提前致谢。
It is unfortunate that I am running into some troubles with the php sax parser and the utf-8 encoding.
The case:
I have a xml-file that is encoded in utf-8. The file is parsed using the standard php sax parser. The data is stored into some container objects and inserted into a mysql database. Unfortunately some characters look weird in the database (mostly german umlaute). For example Gürtel looks like Gürtel.
The following code fragment shows how the parser is instantiated:
$saxParser = xml_parser_create("UTF-8");
Does this suffice to parse utf-8 files? If yes, what I am missing? Some sepcial database stuff when inserting?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
逐步检查编码以查找无效代码:
打印值时,请确保浏览器使用正确的编码读取输出。
您必须确保每个组件都使用正确的编码:
PHP 脚本
将编码设置为 UTF-8 不带 BOM,保存 PHP,因为这可能会导致问题。处理 UTF-8 字符串时,仅使用多字节字符串函数。
XML 文件
XML 文件开头为
并且文件已正确保存,编码设置为 UTF-8。
SQL 列(排序规则)
MySQL 服务器和 PHP 脚本之间的通信
在打开与 MySQL 服务器的连接后立即运行此命令:
Check the encoding step by step to find the invalid code:
When printing the values, make sure your browser reads the output with the correct encoding.
You have to ensure that every component uses the proper encoding:
PHP script
Save your PHP with the encoding set to UTF-8 without BOM, because this might cause problems. Use only multibyte string functions when working with UTF-8 strings.
XML file
XML file starts with
<?xml version="1.0" encoding="UTF-8" ?>
and the file is properly saved with the encoding set to UTF-8.
SQL column (collation)
Communication between MySQL server and PHP script
Run this command right after opening the connection to the MySQL server: