There is an xml file with Unicode encoding that was loaded using the POST method. How can something be changed in it and saved for further use? simplexml_load_file () says that Unicode is not supported. I had to decode myself.

It turned out to decode, but now how to encode? with bad codings. While doing this:

function decode_xml($xml){ $arr = str_split($xml); $text = ''; foreach ($arr as $key => $value) { if(preg_match("/[A-Za-z0-9]|\<|\>|\"|\'|\/|\=|\?|\ |\,|\./", $value)){ $text .= $value; } } $text = str_replace("Unicode", 'Utf-8', $text); $xml = simplexml_load_string($text); return $xml; } function encode_xml($decoded_xml){ //?? } if($_FILES){ $new_path = 'C:\Program Files (x86)\Bloody6\Bloody6\Data\RES\English\SLED\Standard\new2.ckAnimation'; $tmp = $_FILES['file']['tmp_name']; $xml = file_get_contents($tmp); // $xml =   < ? x m l   v e r s i o n = " 1 . 0 "   e n c o d i n g = " U n i c o d e " ? >  $decoded_xml = decode_xml($xml); //раскодировать $xml var_dump($decoded_xml); //Вывести содержимое $encoded_xml = encode_xml($decoded_xml); //Закодировать file_put_contents($new_path, $encoded_xml); //сохранить } 

An example of this xml: https://drive.google.com/open?id=0B58cr0kXJtKIUms1ekxDOEQwNGM

  • and be sure to encode back? - Ipatiev
  • The question is closed. The answer turned out to be simply not to specify the encoding ...-.- - Alexander Myravjev

3 answers 3

Try this.

 $encoded_xml = mb_convert_encoding($encoded_xml, "UTF-8", "auto"); 

If windows-1251 is needed instead of UTF-8, you need to change this value accordingly.

  • Is it unclear that he does not need either windows-1251 or UTF-8? - Ipatiev
  • Option mb_convert_encoding ($ decoded_xml, "Unicode", "Utf-8") seems to be working. But in the header does not change the encoding to Unicode. e n c o d i n g = "U t f - 8 " And if you change it via str_replace before, it returns an empty string - Alexander Myravjev
  • @AlexanderMyravjev I don’t think I understand the nuances of your task, but it can help: header("Content-type: text/html; Charset=utf-8"); - Vad Weber

For encoding there is for example the class https://paulferrett.com/2009/encoding-an-object-in-xml-with-php/

Save next to the script, for example with the name xmlencoder.php

 function encode_xml($decoded_xml){ require ('xmlencoder.php'); $result=XML::encodeObj($decoded_xml); return $result; } 
  • Yes, he does not need to decode. What are you reading in one place? - Ipatiev
  • He has no problem with the encoding. - Ipatiev
  • Well, no, so no), here's an option - Alexey Shatrov
  • In order to create XML in PHP, no third-party libraries are needed. And in order to write an answer, desire alone is not enough. We still have to understand what the question was about. - Ipatiev
  • Need to encode to Unicode - Alexander Myravjev
 function str_split_unicode($str, $l = 0) { if ($l > 0) { $ret = array(); $len = mb_strlen($str, "UTF-8"); for ($i = 0; $i <= $len; $i += $l) { $ret[] = mb_substr($str, $i, $l, "UTF-8"); } return $ret; } return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY); 

}

  • Are you kidding me? This is something to what? - Ipatiev
  • This is a function that recodes the file to you letter by letter, just submit your file as a string. - dpi
  • do you understand English? Can you read what is written in the function name? - Ipatiev
  • the string is unfolded on a unicode; this translation is for you - dpi
  • wrong translation. sit down two - Ipatiev