2012-07-11 38 views
1

当我右键单击浏览器中的xml页面并保存AS时,使用Notepad ++打开它时,它显示OK并显示非英文字符。 但是,如果我编写一个脚本来将页面保存到我的服务器,我遇到了字符编码问题。这真是一件令人头疼的事情。任何帮助?谢谢。php下载xml页面并将其转换为utf-8

function download_page($path) 
{ 
//$path = htmlentities($path); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$path); 
curl_setopt($ch, CURLOPT_FAILONERROR,1); 
    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_TIMEOUT, 280); 
$retValue = curl_exec($ch); 
if (!$retValue){ //echo "erro curl"; 
     }      

@curl_close($ch); 
return $retValue; 
} 

$file= download_page($url); 
$file = mb_convert_encoding($file, 'HTML-ENTITIES', "UTF-8"); 
$file = utf8_encode ($file); 

回答

1

您的代码表明结果以UTF-8编码。首先,你确定这是真的吗?为什么你需要将它转换两次(首先到'HTML-ENTITIES',而不是返回到UTF-8)?如果你只想要html实体,可以使用htmlentities()函数。

+0

我需要非英文字符出现(ě,ú,..) – karto 2012-07-11 13:16:18

+0

如果您有适当的utf-8文件,它们应该会出现。我认为你不需要mb_convert_encoding行。但你如何写出文件?你使用fwrite吗?如果是,请检查[this](http://hu.php.net/manual/en/function.fwrite.php#73764)。这是一个有点旧的帖子,但它可能有帮助。 – ttamas 2012-07-11 14:08:16