\n";
$row = 0;
$cols = 0;
$titles = array();
$handle = @fopen($file, 'r');
if (!$handle) return $handle;
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{
if ($row > 0) $r .= "\t<{$rows}>\n";
if (!$cols) $cols = count($data);
for ($i = 0; $i < $cols; $i++)
{
if ($row == 0)
{
$titles[$i] = $data[$i];
continue;
}
$r .= "\t\t<{$titles[$i]}>";
$r .= $data[$i];
$r .= "{$titles[$i]}>\n";
}
if ($row > 0) $r .= "\t{$rows}>\n";
$row++;
}
fclose($handle);
$r .= "{$container}>";
return $r;
}
?>
Example Usage:
$xml = csv2xml('/home/user/myfile.csv', 'people', 'person');
First parameter is the path to the csv file.
Second parameter is the containing XML tag.
Third parameter is the XML tag that contains each row.
For example, the above code might produce:
John Smith
19100
Jane Doe
19200