Skip to content

Instantly share code, notes, and snippets.

@Fabax
Created February 6, 2013 19:17
Show Gist options
  • Save Fabax/4725002 to your computer and use it in GitHub Desktop.
Save Fabax/4725002 to your computer and use it in GitHub Desktop.
PHP : csvToXml
<?php
$dirStart = "csv";
//$dirEnd = xml
if ($handle = opendir($dirStart)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
$splitted = explode('.', $entry);
if ($splitted[1] == "csv") {
$fichierCsv = $splitted[0].".csv";
$content = buildContent("csv/".$fichierCsv);
$splitted[1] = "xml";
$rebuild = implode('.',$splitted);
//echo "$rebuild<br/>";
writeFile($handle, $rebuild, $content);
}
}
}
closedir($handle);
}
function buildContent($fichierCsv){
$doingInit = true;
$doingPosition = false;
$doingScale = false;
$doingRotation = false;
$lineDone = false;
$content = "";
//variable pour recuperer le nom du fichier
$idTracking = explode('.', $fichierCsv); $idTracking = (string)$idTracking[0];
$idTracking = explode('/', $idTracking); $idTracking = $idTracking[1];
$fichierCsv = str_replace("\\", "", file_get_contents($fichierCsv));
// echo $fichierCsv;
$fichierXml = "tracking.xml";
$content .= "<tracking id=\"".$idTracking."\">\n";
$content .= "\t<init>\n";
$lines = explode ("\n" , $fichierCsv);
//print_r($lines);
//echo count($lines);
for ($i=0; $i <= count($lines); $i++) {
$columns = explode("\t", $lines[$i]);
//print_r($columns);
$lineDone = false;
if ($lines[$i] != "") {
//echo "<br />Ligne ".$i." : ".$lines[$i]."<br />";
for ($j=0; $j <= count($columns) ; $j++) {
if ($lineDone == false && $columns[$j] != "") {
//echo "Colonne ".$j." : ".$columns[$j]."<br />";
if($doingInit == true && $lineDone == false){
if("Source Width" == $columns[$j]){
$content.="\t\t<base width=\"".round($columns[$j+1])."\"></base>\n";
$lineDone = true;
}
if ("Source Height" == $columns[$j]) {
$content.="\t\t<base height=\"".round($columns[$j+1])."\"></base>\n";
$lineDone = true;
}
}
// remplir les balises
if($doingPosition == true && $lineDone == false){
$content.="\t\t<track frame=\"".round($columns[1])."\" x_left=\"".round($columns[2])."\" y_top=\"".round($columns[3])."\"></track>\n";
$lineDone = true;
}
if($doingScale == true && $lineDone == false){
$content.="\t\t<track frame=\"".round($columns[1])."\" x_scale=\"".round($columns[2])."\" y_scale=\"".round($columns[3])."\"></track>\n";
$lineDone = true;
}
if($doingRotation == true && $lineDone == false){
$content.="\t\t<track frame=\"".round($columns[1])."\" degrees=\"".round($columns[2], 2)."\"></track>\n";
$lineDone = true;
}
}
// check position / scale / rotate
if ("Position" == $columns[$j]) {
$content.="\t</init>\n";
$content.="\t<position>\n";
$doingPosition = true;
$doingInit = false;
$lineDone = true;
$i++;
}
if ("Scale" == $columns[$j]) {
$content.="\t</position>\n";
$content.="\t<scale>\n";
$doingScale = true;
$doingPosition = false;
$lineDone = true;
$i++;
}
if ("Rotation" == $columns[$j]) {
$content.="\t</scale>\n";
$content.="\t<rotation>\n";
$doingRotation = true;
$doingScale = false;
$lineDone = true;
$i++;
}
} // fin des collones
}
} // fin des lignes
$content.="\t</rotation>\n";
$content.="</tracking>";
// Cleanup
$content = str_replace("\t\t<track frame=\"0\" x_left=\"0\" y_top=\"0\"></track>\n",'', $content);
$content = str_replace("\t\t<track frame=\"0\" x_scale=\"0\" y_scale=\"0\"></track>\n",'', $content);
$content = str_replace("\t\t<track frame=\"0\" degrees=\"0\"></track>\n",'', $content);
return $content;
// lire nom fichier dans dossier,
}
function writeFile($handle, $rebuild, $content) {
if (!$handle = fopen("xml/".$rebuild, 'w')) {
echo "Impossible d'ouvrir le fichier ($rebuild)";
//exit;
}
// Ecrivons quelque chose dans notre fichier.
if (fwrite($handle, $content) === FALSE) {
echo "Erreur ($rebuild)";
//exit;
} else {
echo $rebuild." OK<br/>";
}
}
?>
<!-- if (file_exists($fichierXml) && !is_writeable($fichierXml)){
echo "fichier non ecrivable";
}
if ($fp = fopen($fichierXml, 'w+')){
fwrite($fp, $content);
fclose($fp);
echo "fichier sauvergard&eacute;";
}
else { echo "fichier corompu"; } -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment