另一个PHP XML解析错误:“输入不正确的UTF-8,指示编码!" [英] Another PHP XML parsing error: "Input is not proper UTF-8, indicate encoding!"

查看:119
本文介绍了另一个PHP XML解析错误:“输入不正确的UTF-8,指示编码!"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

错误:

警告:simplexml_load_string() [function.simplexml-load-string]: 实体:第3行:解析器错误:输入 不正确的UTF-8,表示编码 !字节:0xE7 0x61 0x69 0x73

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 3: parser error : Input is not proper UTF-8, indicate encoding ! Bytes: 0xE7 0x61 0x69 0x73

数据库中的XML(FF中视图源的输出):

XML from database (output from view source in FF):

<?xml version="1.0" encoding="UTF-8" ?><audit><audit_detail>
    <fieldname>role_fra</fieldname>
    <old_value>Role en fran&#xe7;ais</old_value>
    <new_value>Role &#xe7; en fran&#xe7;ais</new_value>
</audit_detail></audit></xml>

如果我理解正确,则该错误与old_value标记中编码的第一个ç有关.确切地说,错误是基于以下字节与此相关的:çais"?

If I understand correctly, the error is related to the first ç encoded in the old_value tag. To be precise, the error is related to this based on the bytes: "çais" ?

这是我加载XML的方式:

Here's how I load the XML:

$xmlData = simplexml_load_string($ed['updates'][$i]['audit_data']);

我使用以下方法循环浏览

The I loop through using this:

foreach ($xmlData->audit_detail as $a){
//code here
}

数据库中的字段为数据类型文本,设置为utf8_general_ci.

The field in the database is of data type text and is set utf8_general_ci.

我用来创建audit_detail存根的函数:

My function to create the audit_detail stubs:

function ed_audit_node($field, $new, $old){


    $old = htmlentities($old, ENT_QUOTES, "UTF-8");
    $new = htmlentities($new, ENT_QUOTES, "UTF-8");

    $out = <<<EOF
        <audit_detail>
            <fieldname>{$field}</fieldname>
            <old_value>{$old}</old_value>
            <new_value>{$new}</new_value>
        </audit_detail>
EOF;
    return $out;
}

在数据库中的插入是这样完成的:

The insert in the database is done like this:

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__));

}

最奇怪的部分是以下内容在一个简单的PHP文件中有效(尽管没有xml声明):

The weirdest part is that the following works (without the xml declaration though) in a simple PHP file:

$testxml = <<<EOF
<audit><audit_detail>
        <fieldname>role_fra</fieldname>
        <old_value>Role en fran&#xe7;ais</old_value>
        <new_value>Role &#xe7; en fran&#xe7;ais</new_value>
    </audit_detail></audit>
EOF;

$ xmlData = simplexml_load_string($ testxml);

$xmlData = simplexml_load_string($testxml);

有人可以帮忙阐明一下吗?

Can someone help shed some light on this?

编辑#1 -我现在正在使用DOM来构建XML文档,并且已经摆脱了错误.在这里起作用:

Edit #1 - I'm now using DOM to build the XML document and have gotten rid of the error. Function here:

$dom = new DomDocument();
$root = $dom->appendChild($dom->createElement('audit'));
$xmlCount = 0;

if($role_fra != $curr['role']['role_fra']){
   $root->appendChild(ed_audit_node($dom, 'role_fra', $role_fra, $curr['role']['role_fra'])); 
   $xmlCount++;
}

...

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;
}

if($xmlCount != 0){
    ed_audit_insert($ed,$dom->saveXML());   
}

但是,我认为我现在有一个显示问题,因为此文本Roééleçséenfranêais"(new_value)显示为:

However, I think I now have a display problem as this text "Roééleç sé en franêais" (new_value) is being displayed as:

显示问题:

在我的HTML文档中,我对内容类型声明了以下内容(不幸的是,我没有在此处进行更改的键):

In my HTML document, I have the following declaration for content-type (unfortunately, I don't hold the keys to make changes here):

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
...
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

我尝试过iconv()转换为ISO-8859-1,但是,大多数特殊字符在转换时都被删除了.使用此命令,剩下的就是"Ro":

I've tried iconv() to convert to ISO-8859-1, however, most of the special characters are being removed when doing the conversion. All that remains is "Ro" using this command:

iconv('UTF-8','ISO-8859-1',$node->new_value);

iconv输出:

iconv output:

数据库中的字段是:utf8_general_ci.但是,连接字符集将是默认的.

The field in the db is: utf8_general_ci. However, the connection charset would be whatever is the default.

不太确定从这里去哪里...

Not quite sure where to go from here...

编辑#2 -我试过utf8_decode看看是否有帮助,但没有帮助.

Edit #2 - I tried utf8_decode to see if that wouldn't help, but it didn't.

utf8_decode($a->new_value);

输出:

我还注意到数据库中的字段确实包含UTF-8.哪个好.

I also noticed that my field in the db did contain UTF-8. Which is good.

推荐答案

&#xe7;为ç"时,您的编码为Windows-1252(或ISO-8859-1),而不是UTF-8.

When &#xe7; is "ç", then your encoding is Windows-1252 (or maybe ISO-8859-1), but not UTF-8.

这篇关于另一个PHP XML解析错误:“输入不正确的UTF-8,指示编码!"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆