我在 MySQL 中有一个文本类型的字段,使用以下排序规则:utf8_general_ci
。
此 XML 字段使用使用 DOMDocument 构建的变量填充:
function ed_audit_node($dom, $field, $new, $old){
//create audit_detail node
$ad = $dom->createElement('audit_detail');
$fn = $dom->createElement('fieldname');
$fn->appendChild($dom->createTextNode($field));
$ad->appendChild($fn);
$ov = $dom->createElement('old_value');
$ov->appendChild($dom->createTextNode($old));
$ad->appendChild($ov);
$nv = $dom->createElement('new_value');
$nv->appendChild($dom->createTextNode($new));
$ad->appendChild($nv);
//append to document
return $ad;
}
这是我保存到数据库的方法( $xml 来自 $dom->saveXML() ):
function ed_audit_insert($ed, $xml){
global $visitor;
$sql = <<<EOF
INSERT INTO ed.audit
(employee_id, audit_date, audit_action, audit_data, user_id)
VALUES (
{$ed[emp][employee_id]},
now(),
'{$ed[audit_action]}',
'{$xml}',
{$visitor[user_id]}
);
EOF;
$req = mysql_query($sql,$ed['db']) or die(db_query_error($sql,mysql_error(),__FUNCTION__));
//snip
}
查看一个较旧的、平行的、稍微相关的线程,了解我如何创建此 XML: Another PHP XML parsing error: "Input is not proper UTF-8, indicate encoding!"
有效的方法: - 查询数据库,选择字段并使用 jQuery (.ajax()) 输出它并填充文本区域。 Firebug 和文本区域与数据库中的内容匹配(通过 Toad 确认)。
什么不起作用: - 将数据库中的文本输出到 HTML 页面中。此 HTML 页面的内容类型为 ISO-8859-1,我无法更改。
这是将其输出到屏幕的代码:
$xmlData = simplexml_load_string($d['audit_data']);
foreach ($xmlData->audit_detail as $a){
echo "<p> straight from db = ".$a->new_value."</p>";
echo "<p> utf8_decode() = ".utf8_decode($a->new_value)."</p>";
}
我还为 Firefox 使用了一个字符集转换器扩展:尝试了 ISO-8859-1、UTF-8 和 1252 但没有成功。
如果它是 UTF-8,我不应该看到里面有问号的菱形吗(因为它是 content-type = ISO-8859-1)?如果不是 UTF-8,那是什么?
编辑#1
这是我所做的其他测试的快照:
$xmlData = simplexml_load_string($d['audit_data']);
foreach ($xmlData->audit_detail as $a){
echo "<p>encoding is, straight from db, using mb_detect_encoding: ".mb_detect_encoding($a->new_value)."</p>";
echo "<p>encoding is, with utf8_decode, using mb_detect_encoding: ".mb_detect_encoding(utf8_decode($a->new_value))."</p>";
echo "<hr/>";
echo "<p> straight from db = <pre>".$a->new_value."</pre></p>";
echo "<p> utf8_decode() = <pre>".utf8_decode($a->new_value)."</pre></p>";
echo "<hr/>";
$iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $a->new_value);
$iso88591_3 = mb_convert_encoding($a->new_value, 'ISO-8859-1', 'UTF-8');
echo "<p> iconv() = ".$iso88591_2."</p>";
echo "<p> mb_convert_encoding() = ".$iso88591_3."</p>";
}
编辑 #2
我添加了 FF 专有标签 xmp。
代码:
$xmlData = simplexml_load_string($d['audit_data']);
foreach ($xmlData->audit_detail as $a){
echo "<p>encoding is, straight from db, using mb_detect_encoding: ".mb_detect_encoding($a->new_value)."</p>";
echo "<p>encoding is, with utf8_decode, using mb_detect_encoding: ".mb_detect_encoding(utf8_decode($a->new_value))."</p>";
echo "<hr/>";
echo "<p> straight from db = <pre>".$a->new_value."</pre></p>";
echo "<p> utf8_decode() = <pre>".utf8_decode($a->new_value)."</pre></p>";
echo "<hr/>";
$iso88591_2 = iconv('UTF-8', 'ISO-8859-1', $a->new_value);
$iso88591_3 = mb_convert_encoding($a->new_value, 'ISO-8859-1', 'UTF-8');
echo "<p> iconv() = ".$iso88591_2."</p>";
echo "<p> mb_convert_encoding() = ".$iso88591_3."</p>";
echo "<hr/>";
echo "<p>straight from db, using <xmp> = <xmp>".$a->new_value."</xmp></p>";
echo "<p>utf8_decode(), using <xmp> = <xmp>".utf8_decode($a->new_value)."</xmp></p>";
}
以下是页面中的一些元标记:
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="dc.language" scheme="ISO639-2/T" content="eng" />
IMO,最后一个元标记没有意义。
编辑 #3
源代码:
<p>encoding is, straight from db, using mb_detect_encoding: UTF-8</p><p>encoding is, with utf8_decode, using mb_detect_encoding: ASCII</p><hr/><p> straight from db = <pre>Ro马eç ³é ¥n franê¡©s</pre></p><p> utf8_decode() = <pre>Ro?e??n fran?s</pre></p><hr/><p> iconv() = Ro</p><p> mb_convert_encoding() = Ro?e??n fran?s</p><hr/><p>straight from db, using <xmp> = <xmp>Ro马eç ³é ¥n franê¡©s</xmp></p><p>utf8_decode(), using <xmp> = <xmp>Ro?e??n fran?s</xmp></p>
编辑 #4
这是进入数据库的 SQL 语句:
INSERT INTO ed.audit
(employee_id, audit_date, audit_action, audit_data, user_id)
VALUES (
75,
now(),
'u',
'<?xml version="1.0"?>
<audit><audit_detail><fieldname>role_fra</fieldname><old_value>aRo马e砳頥n franꡩs</old_value><new_value>bRo马e砳頥n franꡩs</new_value></audit_detail></audit>
',
333
);
!请注意,此 XML 中的文本不一定与上面提供的屏幕截图匹配。
编辑#5
这是我的新函数,它将 CDATA 标签包装在我的旧值和新值节点的值周围:
function ed_audit_node($dom, $field, $new, $old){
//create audit_detail node
$ad = $dom->createElement('audit_detail');
$fn = $dom->createElement('fieldname');
$fn->appendChild($dom->createTextNode($field));
$ad->appendChild($fn);
$ov = $dom->createElement('old_value');
$ov->appendChild($dom->createCDATASection($old));
$ad->appendChild($ov);
$nv = $dom->createElement('new_value');
$nv->appendChild($dom->createCDATASection($new));
$ad->appendChild($nv);
//append to document
return $ad;
}
我还在 XML 文档中添加了编码:
$dom = new DomDocument('1.0', 'UTF-8');
这是我新的 simpleXML 调用:
$xmlData = simplexml_load_string($d['audit_data'], "SimpleXMLElement", LIBXML_NOENT | LIBXML_NOCDATA);
我在 Toad 中也看到了 CDATA 标签。但是,我仍然收到错误消息:
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 2: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xE9 0xE9 0x6C 0x65 in <snip>
编辑 #6
我刚刚注意到 jQuery 调用在 CDATA 中返回了正确的重音字符。
最佳答案
从技术上讲,您的字符串是 UTF8 编码的,但 HTML 编码的字符(当由浏览器呈现时)不是 UTF8 编码的。所以ꡩ
是有效的 UTF8 字符串,但从 Web 浏览器呈现到屏幕上的字符不是有效的 UTF8。
我也会像这样将您的回显包装到屏幕上(您示例中的最后两行):
echo "<p>straight from db = <xmp>".$a->new_value."</xmp></p>";
echo "<p>utf8_decode() = <xmp>".utf8_decode($a->new_value)."</xmp></p>";
这将清楚地显示我在上面提出的观点。
编辑:
问题实际上是 PHP 的 simplexml_load_string() 中无法控制的未记录的“功能”。它会自动将所有字符从其 XML 实体形式转换为实际的字符形式。避免这种情况的唯一方法是像这样使用 simplexml_load_string():
$data = simplexml_load_string(
'<?xml version="1.0" encoding="utf-8"?>
<audit>
<audit_detail>
<fieldname>role_fra</fieldname>
<old_value><![CDATA[aRo马e砳頥n franꡩs]]></old_value>
<new_value><![CDATA[bRo马e砳頥n franꡩs]]></new_value>
</audit_detail>
</audit>',
"SimpleXMLElement",
LIBXML_NOENT | LIBXML_NOCDATA
);
print "<PRE>";
print_r($data);
exit;
您必须将元素包装在 <![CDATA[]]>
中标记,然后将 LIBXML_NOCDATA 选项传递给 xml 解析器。这将强制 <![CDATA[]]>
中的内容将标记转换为字符串类型,PHP 可以在 SimpleXMLObject 之外正确处理它。
关于PHP、MySQL 和 XML = 乱码 HTML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4683291/