php utf-8编码问题
大家好: 我在这里遇到了一个棘手的问题:我需要读取一些文件并将其内容转换为一些 XML 文件。对于文件中的每一行,我相信其中大多数都是有效的 ASCII 代码,因此我可以将该行读入 php 并将其保存到 XML 文件中,默认编码 XML 为“UTF-8”。但是,我注意到原始文件中可能存在一些GBK、GB2312(汉字)、SJIS(日文字符)等,php直接将字符串保存到XML中没有问题。但是,XML 解析器会检测到无效的 UTF-8 代码并崩溃。
现在,我认为最适合我目的的 php 库函数可能是:
$decode_str = mb_convert_encoding($str, 'UTF-8', 'auto');
在将每一行插入 XML 之前,我尝试为每一行运行此对话函数。但是,当我使用一些 UTF-16 和 GBK 编码进行测试时,我认为该函数无法正确区分输入字符串编码模式。
另外,我尝试使用 CDATA 来包装字符串,奇怪的是 XML 解析器仍然抱怨无效的 UTF-8 代码等。当然,当我 vim xml 文件时,CDATA 里面的内容肯定是一团糟。
有什么建议吗?
Hi All:
I met a tricky problem here: I need to read some files and convert its content into some XML files. For each line in the file, I believe most of them are valid ASCII code, so that I could just read the line into php and save the line into an XML file with default encoding XML as 'UTF-8'. However, I noticed that there might be some GBK, GB2312(Chinese character), SJIS(Japanese characters) etc.. existed in the original files, php have no problems to save the string into XML directly. However, the XML parser will detect there are invalid UTF-8 codes and crashed.
Now, I think the best library php function for my purpose is probably:
$decode_str = mb_convert_encoding($str, 'UTF-8', 'auto');
I try to run this conversation function for each line before inserting it into XML. However, as I tested with some UTF-16 and GBK encoding, I don't think this function could correctly discriminate the input string encoding schema.
In addition, I tried to use CDATA to wrap the string, it's weird that the XML parser still complain about invalid UTF-8 codes etc.. of course, when I vim the xml file, what's inside the CDATA is a mess for sure.
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我曾经花了很多时间创建一个安全的 UTF8 编码函数< /a>:
主要问题是找出输入字符串已经使用的编码。请告诉我我的解决方案是否也适合您!
I spend once a lot of time to create a safe UTF8 encoding function:
The main problem was to figure out which encoding the input string is already using. Please tell me if my solution works for you as well!
我在使用 json_encode 时遇到了这个问题。我用它把所有东西都转成utf8。
来源: https://www.php.net/manual/en/ function.json-encode.php
I ran into this problem while using json_encode. I use this to get everything into utf8.
Source: https://www.php.net/manual/en/function.json-encode.php