PHP SimpleXML asXML写入ANSI编码的文件 [英] PHP SimpleXML asXML writes ANSI encoded file
问题描述
我正在尝试将一些内容写到XML文件中,但是我确实遇到了特殊字符的问题.
I am trying to write some content into an XML file, yet I do have problems with special characters.
我要编写的内容是通过$_GET
提交给脚本的,因此我认为它已正确解码为UTF-8内容.
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'];
将以类似的方式进食
file.php?content=s%F6per
在PHP中,我执行以下操作:
In the PHP I do the following:
$xml = simplexml_load_file('file.xml');
$newentry = $xml -> addChild('element',$write);
$xml -> asXML($xml_filename);
打开的XML文件是UTF-8编码的.当我编写没有任何问题字符"的内容时,asXML会再次将文件保存为UTF-8.一旦我插入特殊字符,它就会以ANSI编码格式保存,因为我将无法打开它而使文件混乱(脚本会抱怨编码不正确).
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.
我想念什么?阅读手册给我的印象是,我应该做的一切都很好(即不要弄乱$_GET['content']
),因此很遗憾,我没有任何线索.
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.
非常感谢!
推荐答案
您的特殊字符看起来不是UTF-8,而是ISO-8859-1字符
your special characters does not look a UTF-8, but a ISO-8859-1 character
请参阅此处- http://www.degraeve.com/reference/urlencoding.php
可能的解决方案
$newentry = $xml->addChild('element', htmlentities($write));
或
$newentry = $xml->addChild('element', iconv('ISO-8859-1', 'UTF-8', $write));
而且离题,请避免使用$_GET
将某些内容写入文件或插入数据库,这是有风险的
And off-topic, please avoid using $_GET
to write something into file or insert into database, is risky
这篇关于PHP SimpleXML asXML写入ANSI编码的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!