PHP error iconv

When I create an XML file with an ASP Classic script and import the XML file into a PHP page, the import process works fine.

But when I create the same XML through a PHP script (instead of ASP Classic) and use it in the same import process, it does not work.

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

I noticed in my import process:

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

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

+6
source share
2 answers

Resources: PHP Official Website - 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 an 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 want 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> 

here is 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 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(); ?> 
+4
source

What happens when you do:

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

?

If the process does not work at the point you specify, then it does not perform the conversion from UTF-16 to UTF-8, which means that you have one or more characters in the input string in which there is no UTF-8 representation. The // IGNORE flag silently deletes these characters, which is clearly bad, but using this flag can help determine that the problem is actually what I think. You can also try transliterating characters that are not executed:

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

The characters will be approximated, so you save something, at least. See an example here: http://www.php.net/manual/en/function.iconv.php

All of this suggests that UTF-16 is an acceptable encoding for XML content. Why do you want to convert it?

0
source

Source: https://habr.com/ru/post/955290/


All Articles