2012-10-14 177 views
0

我有这样一个简单的XML脚本:SimpleXML特殊字符?

$file='example.xml'; 
if (file_exists($file)) { 
    $xml = simplexml_load_file($file); 
    $f = fopen('newfile.csv', 'w'); 

    // array to hold the field names 
    $headers = array(); 
    // loop through the first set of fields to get names 
    foreach ($xml->Information->children() as $field) { 
     // put the field name into array 
     $headers[] = $field->getName(); 
    } 
    // print headers to CSV 
    fputcsv($f, $headers, ',', '"'); 

    foreach ($xml->Information as $information) { 
     fputcsv($f, get_object_vars($information), ',', '"'); 
    } 
    fclose($f); 
} 

但如果example.xml中有一个£标志,那么在newfile.csv它会显示为£

有什么办法可以解决这个问题吗?我不知道example.xml的编码,因为它是一个远程文件。

+1

*它会显示为£* ...在哪里(其中应用程序)? –

+0

例如,如果我在windows下在excel中下载'newfile.csv'。 –

回答

0

一种方法是通过检测文件中的编码,通过在卷曲会话中处理标头。应该有一个Content-Type。否则,xml规范将utf-8定义为基本字符集,因此您可以尝试使用它。

如果您不想使用卷曲,您也可以使用mb_detect_encoding,但我会尝试先使用它。

然后,如果需要,可以使用iconvmb_convert_encoding将类型从utf-8或您喜欢的字符集更改为任何类型,然后保存该文件。

再见