PHP Sax 解析器和 UTF-8

发布于 2024-12-17 17:51:44 字数 354 浏览 1 评论 0原文

不幸的是,我在 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 技术交流群。

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

发布评论

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

评论(1

泼猴你往哪里跑 2024-12-24 17:51:44

逐步检查编码以查找无效代码:

  1. 打印从 XML 检索的值
  2. 打印出您构建的 SQL 语句

打印值时,请确保浏览器使用正确的编码读取输出。

您必须确保每个组件都使用正确的编码:

PHP 脚本

将编码设置为 UTF-8 不带 BOM,保存 PHP,因为这可能会导致问题。处理 UTF-8 字符串时,仅使用多字节字符串函数

XML 文件

XML 文件开头为

并且文件已正确保存,编码设置为 UTF-8。

SQL 列(排序规则)

VARCHAR(length) [CHARACTER SET charset_name] [COLLATE collation_name]

MySQL 服务器和 PHP 脚本之间的通信

在打开与 MySQL 服务器的连接后立即运行此命令:

SET NAMES 'UTF8'

SET NAMES 指示客户端将使用什么字符集发送 SQL
到服务器的语句。
http://dev.mysql.com/doc/refman/5.0 /en/charset-connection.html

Check the encoding step by step to find the invalid code:

  1. Print the value you retrive from the XML
  2. Print out the SQL statement you build

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)

VARCHAR(length) [CHARACTER SET charset_name] [COLLATE collation_name]

Communication between MySQL server and PHP script

Run this command right after opening the connection to the MySQL server:

SET NAMES 'UTF8'

SET NAMES indicates what character set the client will use to send SQL
statements to the server.
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html

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