PHP SimpleXML asXML writes ANSI encoded file

人走茶凉 提交于 2020-01-15 02:37:46

问题


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



来源:https://stackoverflow.com/questions/6940104/php-simplexml-asxml-writes-ansi-encoded-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!