简体   繁体   English

PHP iconv错误

[英]PHP iconv error

When I generate an XML file with an ASP Classic script, and import the XML file in a PHP page, the import process works fine. 当我使用ASP Classic脚本生成XML文件,并在PHP页面中导入XML文件时,导入过程正常。

But, when I generate the same XML through a PHP script (instead of ASP Classic) and use it in the same import process, then it's not working. 但是,当我通过PHP脚本(而不是ASP Classic)生成相同的XML并在同一个导入过程中使用它时,它就无法正常工作。

$xml = iconv("UTF-16", "UTF-8", $xml);

I noticed in my import process: 我在导入过程中注意到:

  • before the $xml = iconv("UTF-16", "UTF-8", $xml); $xml = iconv("UTF-16", "UTF-8", $xml); line in my code, the XML file is in the proper format. 在我的代码中,XML文件的格式正确。
  • But after the $xml = iconv("UTF-16", "UTF-8", $xml); 但是在$xml = iconv("UTF-16", "UTF-8", $xml); line, the XML file is corrupted. 行,XML文件已损坏。

When I comment this line of code out and use the PHP XML file, then it works fine. 当我将这行代码注释掉并使用PHP XML文件时,它可以正常工作。

Resources: PHP official site- SimpleXMLElement documentation 资源: PHP官方站点 - SimpleXMLElement文档

If you claim that there is an error in this line: 如果您声称此行中存在错误:

$xml = iconv("UTF-16", "UTF-8", $xml);

then change it to this, because $xml is probably not "UTF-16": 然后将其更改为此,因为$ xml可能不是“UTF-16”:

$xml = iconv(mb_detect_encoding($xml), "UTF-8", $xml);

To save the XML file: 要保存XML文件:

//saving generated xml file
$xml_student_info->asXML('file path and name');

To import the xml file: 要导入xml文件:

$url = "http://www.domain.com/users/file.xml";
$xml = simplexml_load_string(file_get_contents($url));

If you have an array as follows: 如果你有一个数组如下:

$test_array = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'another_array' => array (
    'stack' => 'overflow',
  ),
);

and you wish to convert it to the following XML: 并且您希望将其转换为以下XML:

<?xml version="1.0"?>
<main_node>
    <bla>blub</bla>
    <foo>bar</foo>
    <another_array>
        <stack>overflow</stack>
    </another_array>
</main_node>

then here's the PHP code: 那么这是PHP代码:

<?php

//make the array
$test = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'another_array' => array (
    'stack' => 'overflow',
  ),
);   

//make an XML object
$xml_test = new SimpleXMLElement("<?xml version=\"1.0\"?><main_node></main_node>");

// function call to convert array to xml
array_to_xml($test,$xml_test);

//here's the function definition (array_to_xml)
function array_to_xml($test, &$xml_test) {
    foreach($test as $key => $value) {
        if(is_array($value)) {
            if(!is_numeric($key)){
                $subnode = $xml_test->addChild("$key");
                array_to_xml($value, $subnode);
            }
            else{
                $subnode = $xml_test->addChild("item$key");
                array_to_xml($value, $subnode);
            }
        }
        else {
            $xml_test->addChild("$key","$value");
        }
    }
}

/we finally print it out
print $xml_test->asXML();

?>

What happens when you do: 当你这样做时会发生什么:

$xml = iconv("UTF-16", "UTF-8//IGNORE", $xml);

?

If the process is failing at the point you've identified, then it is failing conversion from UTF-16 to UTF-8, which means you have one or more characters in the input string that do not have a UTF-8 representation. 如果进程在您识别的位置失败,那么它将无法从UTF-16转换为UTF-8,这意味着您在输入字符串中有一个或多个没有UTF-8表示的字符。 The "//IGNORE" flag will silently drop those characters, which is obviously bad, but using that flag can be helpful in pinpointing if what I think is the problem is actually the case. “// IGNORE”标志将默默地删除这些字符,这显然很糟糕,但使用该标志可以帮助确定我认为问题实际上是什么情况。 You could also try transliterating the characters that fail: 您还可以尝试音译失败的字符:

$xml = iconv("UTF-16", "UTF-8//TRANSLIT", $xml);

The characters will be approximated, so you'll retain something, at least. 角色将近似,所以你至少会保留一些东西。 See the example here: http://www.php.net/manual/en/function.iconv.php 请参阅此处的示例: http//www.php.net/manual/en/function.iconv.php

All of that said, UTF-16 is an acceptable charset for XML content. 所有这些都表明,UTF-16是XML内容的可接受字符集。 Why are you wanting to convert it? 你为什么要转换它?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM