I have a simple XML script like this:
$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);
}
But if example.xml has a £ sign in it, then in newfile.csv it will show as £.
Is there any way to overcome this? I dont know the encoding of example.xml since it is a remote file that is wget’d in.
One way to do it is by detecting the encoding in the file, which you can do by processing the headers in a curl session. There should be a
Content-Type. Otherwise, the xml specs, define utf-8 as the basic character set, so you can try to use that.You may also use
mb_detect_encodingif you don’t want to use curl, but I’ll try to use it first.Then, if you want, you can use
iconvormb_convert_encodingto change the type from whatever it is to utf-8 or the charset you prefer and then save the file.Bye