PHP-使用SimpleXml保存到XML文件时的编码问题
我正在努力解决PHP应用程序中的编码问题,该应用程序:PHP-使用SimpleXml保存到XML文件时的编码问题,php,xml,encoding,utf-8,google-translate,Php,Xml,Encoding,Utf 8,Google Translate,我正在努力解决PHP应用程序中的编码问题,该应用程序: 读取XML文件并根据某些规则对其进行解析 调用Google Translate API并使用结果填充 以后用于在浏览器上显示数据的数据库(即 (部分工作正常) 将数据保存到一个XML文件(它保存了数据,但有问题 使用编码) 这些数据来自以UTF-8编码的Google Translate和浏览器,只要你有正确的标题,不管是哪种语言,它都能很好地显示出来 以下是谷歌翻译功能: function mt($text, $lang) {
function mt($text, $lang) {
$url = 'https://www.googleapis.com/language/translate/v2?key=' . $apiKey . '&q=' . rawurlencode($text) . '&source=en&target=' . $lang;
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($handle);
$responseDecoded = json_decode($response, JSON_UNESCAPED_UNICODE);
$responseCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if($responseCode != 200) {
$resultxt = 'not200result';
}
else {
$resultxt = $responseDecoded['data']['translations'][0]['translatedText'];
}
return $resultxt;
}
我正在使用Simplexml加载一个XML文件,修改其内容并用asXml()保存它。
生成的XML文件以UTF-8以外的格式编码,如下所示:
<value>ようこそ%0 ST数学</value>
我尝试过使用HTMLEntities()并使用utf8_encode()和utf8_decode(),但无法使其工作
我试过了所有的方法,也看过很多其他的帖子。就我个人而言,我无法理解这一点。感谢您的帮助 实际上是UTF8HTML实体编码。尝试html_实体_解码功能。事实上,这个问题有点基本。。。字符串不必从Google Translate转换,因为它已经是UTF-8格式了。我在XML序言中缺少encoding=“utf-8”信息。当未声明编码信息时,它将内容解释为ISO 8859-1。
$xml=simplexml_load_file('myfile.xml'); //Load source XML file
$xml->addAttribute('encoding', 'UTF-8');
$xmlFile = 'translation.xml'; //File that will be saved
//Here I have a call to the MT function above and get it to the XML file at face value.
$xml->asXML($xmlFile) //save translated XML file