PHP SimpleXML asXML writes ANSI encoded file

2019-07-02 11:22发布

问题:

I am trying to write some content into an XML file, yet I do have problems with special characters.

The content I'd like to write is submitted to the script via $_GET, so I assume it is properly decoded into UTF-8 content.

$write = $_GET['content'];

will be fed like:

file.php?content=s%F6per

In the PHP I do the following:

$xml = simplexml_load_file('file.xml');
$newentry = $xml -> addChild('element',$write);
$xml -> asXML($xml_filename);

The XML file that is opened is UTF-8 encoded. When I write content without any of those "problem characters" the asXML will save the file in UTF-8 again. As soon as I insert special characters it gets saved in ANSI encoding, messing up the file as I won't be able to open it (the script will complain about improper encoding) anymore.

What am I missing? Reading the manual gives me the impression that I should be doing everything ok (i.e. not messing with the $_GET['content']), so I unfortunately have no clue.

Thanks so much!

回答1:

your special characters does not look a UTF-8, but a ISO-8859-1 character

see here - http://www.degraeve.com/reference/urlencoding.php

Possible solution

$newentry = $xml->addChild('element', htmlentities($write));

Or

$newentry = $xml->addChild('element', iconv('ISO-8859-1', 'UTF-8', $write));

And off-topic, please avoid using $_GET to write something into file or insert into database, is risky