PHP的XML字符编码问题

时间:2022-08-03 21:36:54

I have code which is creating an XML, my only problem is with the encoding of words like á, olá and ção.
These characters dont appear correctly and when I try reading the XML I get an error displayed relating to that character.

我有创建XML的代码,我唯一的问题是对á,olá和ção等单词的编码。这些字符没有正确显示,当我尝试读取XML时,我得到一个与该字符有关的错误。

$dom_doc = new DOMDocument("1.0", "utf-8");
$dom_doc->preserveWhiteSpace = false;
$dom_doc->formatOutput = true;
$element = $dom->createElement("hotels");

while ($row = mysql_fetch_assoc($result)) {

$contact = $dom_doc->createElement( "m" . $row['id'] );

$nome = $dom_doc->createElement("nome", $row['nome'] );

$data1 = $dom_doc->createElement("data1", $row['data'] );
$data2 = $dom_doc->createElement("data2", $row['data2'] );


$contact->appendChild($nome);
$contact->appendChild($data1);
$contact->appendChild($data2);

$element->appendChild($contact);
$dom_doc->appendChild($element);

What can I change to fix my problem, I am using utf-8???

我可以改变什么来解决我的问题,我使用的是utf-8 ???

2 个解决方案

#1


0  

You are using utf-8, the 8-bit unicode encoding format. Even though it properly supports all 1,112,064 code points in Unicode its possible that there is an issue here.
Try UTF-16 as the standard, just an idea. See below:

您使用的是8位unicode编码格式的utf-8。即使它正确支持Unicode中的所有1,112,064个代码点,也可能存在问题。尝试使用UTF-16作为标准,只是一个想法。见下文:

$dom_doc = new DOMDocument("1.0", "utf-16");

OR

$dom_doc = new DOMDocument("1.0", "ISO-10646");

#2


1  

Please try to put directly 'á', 'olá' or 'ção' in your script.

请尝试直接在脚本中放入'á','olá'或'ção'。

$data1 = $dom_doc->createElement("data1", 'ção');

If you don't have problem, this is probably the data you get from mysql that are wrongly encoded. Are you sure your mysql outputs correct UTF-8?

如果您没有问题,这可能是您从mysql获取的错误编码的数据。你确定你的mysql输出正确的UTF-8?

To know that, make your PHP dump your data in an HTML document with meta tag set to UTF-8 and see if the characters display correctly.

要知道这一点,让PHP将数据转储到HTML文档中,并将元标记设置为UTF-8,并查看字符是否正确显示。

You can also call :

你也可以打电话:

$data1 = $dom_doc->createElement("data1", mb_detect_encoding($row['data']));

and see what encoding is detected by PHP for your data.

并查看PHP检测到的数据编码。

If you can't convert the data from your database, or change its settings, you can use mb_convert to do it on-the-fly : http://www.php.net/manual/en/function.mb-convert-encoding.php

如果您无法转换数据库中的数据或更改其设置,则可以使用mb_convert即时执行此操作:http://www.php.net/manual/en/function.mb-convert- encoding.php

#1


0  

You are using utf-8, the 8-bit unicode encoding format. Even though it properly supports all 1,112,064 code points in Unicode its possible that there is an issue here.
Try UTF-16 as the standard, just an idea. See below:

您使用的是8位unicode编码格式的utf-8。即使它正确支持Unicode中的所有1,112,064个代码点,也可能存在问题。尝试使用UTF-16作为标准,只是一个想法。见下文:

$dom_doc = new DOMDocument("1.0", "utf-16");

OR

$dom_doc = new DOMDocument("1.0", "ISO-10646");

#2


1  

Please try to put directly 'á', 'olá' or 'ção' in your script.

请尝试直接在脚本中放入'á','olá'或'ção'。

$data1 = $dom_doc->createElement("data1", 'ção');

If you don't have problem, this is probably the data you get from mysql that are wrongly encoded. Are you sure your mysql outputs correct UTF-8?

如果您没有问题,这可能是您从mysql获取的错误编码的数据。你确定你的mysql输出正确的UTF-8?

To know that, make your PHP dump your data in an HTML document with meta tag set to UTF-8 and see if the characters display correctly.

要知道这一点,让PHP将数据转储到HTML文档中,并将元标记设置为UTF-8,并查看字符是否正确显示。

You can also call :

你也可以打电话:

$data1 = $dom_doc->createElement("data1", mb_detect_encoding($row['data']));

and see what encoding is detected by PHP for your data.

并查看PHP检测到的数据编码。

If you can't convert the data from your database, or change its settings, you can use mb_convert to do it on-the-fly : http://www.php.net/manual/en/function.mb-convert-encoding.php

如果您无法转换数据库中的数据或更改其设置,则可以使用mb_convert即时执行此操作:http://www.php.net/manual/en/function.mb-convert- encoding.php