简体   繁体   中英

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.

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.

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

I noticed in my import process:

  • before the $xml = iconv("UTF-16", "UTF-8", $xml); line in my code, the XML file is in the proper format.
  • But after the $xml = iconv("UTF-16", "UTF-8", $xml); line, the XML file is corrupted.

When I comment this line of code out and use the PHP XML file, then it works fine.

Resources: PHP official site- SimpleXMLElement documentation

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 = iconv(mb_detect_encoding($xml), "UTF-8", $xml);

To save the XML file:

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

To import the xml file:

$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 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

//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. 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. 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

All of that said, UTF-16 is an acceptable charset for XML content. Why are you wanting to convert it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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