0

I have read a XML file with the simplexml_load_file() function. I suppose this function is well written and supports XML encodings correctly. My XML file is in UTF-8 format, i.e. it contains normal ANSI characters along with national characters with multibyte encoding.

So, now I want to write the XML file back with fopen() and fwrite(), also in UTF-8 format.

Should I perform some conversions to do that correctly?

Suppose variable $a contains some UTF-8 encodings. Will it be written correctly?

Expedito
  • 7,771
  • 5
  • 30
  • 43
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • You don't have to go for `fopen` and `fwrite` if you're already using SimpleXML. `public mixed SimpleXMLElement::asXML ([ string $filename ] )` –  Jul 15 '12 at 15:15
  • I have read file with `simplexml`, but then abandoned this library and use my own arrays and structures to hold data. So, to write data I need either construct XML in memory first, or just write data into a file. Latter looks simpler. – Suzan Cioc Jul 15 '12 at 15:24
  • possible duplicate of [How to write file in UTF-8 format?](http://stackoverflow.com/questions/4839402/how-to-write-file-in-utf-8-format) – Dejan Marjanović Jul 15 '12 at 21:30
  • This has already been answered in a previous question: http://stackoverflow.com/questions/4839402/how-to-write-file-in-utf-8-format Good Luck! – TurdPile Jul 15 '12 at 15:10

1 Answers1

0

if your xml is really encoded in utf8 already, then the strings produced by simplexml will be utf8 also. meaning- just write them to the file as-is.

php's interfacing with the underlying libxml is a bit funky though. Even though the xml may be utf8 encoded, make sure that the xml starts with a proper encoding declaration or it may get misinterpreted.

<?xml version="1.0" encoding="UTF-8"?>
goat
  • 31,486
  • 7
  • 73
  • 96