1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?php
setlocale(LC_TIME, "fr_FR");
unlink("./data/updatedb.csv");
unlink("./data/stock.csv");
rename("./data/UPDATEDB.TXT", "./data/updatedb.csv");
$newfichier = fopen("./data/sortie_stock.csv", "w");
// crée le fichier cible s'il n'existe pas ou écrase s'il existe déjà
fclose($newfichier);
$lines = file('./data/updatedb.csv'); // ouverture du fichier d'origine
$fp = fopen("./data/stock.csv","w"); //création du fichier de sortie
$tampon = ""; // initialisation de la variable tampon
foreach ($lines as $lineNumber => $lineContent) // boucle pour parcourir le fichier d'origine
{
if (strpos($lineContent,"#<SEPARATOR>#") !== false) // condition si la ligne contient séparateur
{
$tampon = $tampon . $lineContent; // ajout du contenu de la ligne au tampon
$arr_base = explode(";",$tampon);
$uc_12 = $arr_base[12];
$ucfirst = ucfirst(mb_strtolower($uc_12));
$tampon = str_replace($uc_12,$ucfirst,$tampon);
$uc_13 = $arr_base[13];
$ucfirst = ucfirst(mb_strtolower($uc_13));
$tampon = str_replace($uc_13,$ucfirst,$tampon);
$uc_15 = $arr_base[15];
$ucfirst = ucfirst(mb_strtolower($uc_15));
$tampon = str_replace($uc_15,$ucfirst,$tampon);
$uc_17 = $arr_base[17];
$ucfirst = ucfirst(mb_strtolower($uc_17));
$tampon = str_replace($uc_17,$ucfirst,$tampon);
$uc_18 = $arr_base[18];
$ucfirst = ucfirst(mb_strtolower($uc_18));
$tampon = str_replace($uc_18,$ucfirst,$tampon);
$uc_10 = $arr_base[10];
$date = strftime("%d %B %Y", strtotime($uc_10));
$tampon = str_replace($uc_10,$date,$tampon);
fputs($fp,$tampon); // écriture de la ligne tampon dans le fichier
$tampon = ""; // clear du tampon
}else{ // condition si ne contient pas de séparateur
$tampon = $tampon . trim($lineContent); // alors ajoute le contenu au tampon déjà existant
}
}
fclose($fp); // fermeture du fichier
file_put_contents('./data/sortie_stock.csv', file_get_contents('./data/format.csv') . file_get_contents('./data/stock.csv') );
?> |
Partager