j'ai telecharger ce code pour extraire un fichier log et le mettre sous forme de tableau.
je voudrais savoir comment cela fonctionne car apparament le fichier log est en une seule ligne...
De plus la finalité de cette question est la suivante. de quel façon puis je reprendre chaque donnée independament et les afficher sur une page apres avoir upload le fichier log.
Version Normale avec affichage de formulaire
Code : Tout sélectionner
<?
// Update 28-12-08
// Added variable to hold date and time of when the log was created
//
if ($_POST["data_in"]) { // Check if the data has been posted
// Function to find the guild data within the log
function substring_between($haystack,$start,$end) {
if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) {
return false;
} else {
$start_position = strpos($haystack,$start)+strlen($start);
$end_position = strpos($haystack,$end);
return substr($haystack,$start_position,$end_position-$start_position);
}
}
// Get time and date of when the log was created
$gdata = substring_between($_POST["data_in"],'{1{','}1}'); // Get guild data between the tags and dump the rest of the log
$gdata = stripslashes($gdata);
$gde_date = substr($_POST["data_in"], 0, 10); // Get date
$gde_time = substr($_POST["data_in"], 10, 10); // Get time
$toremove = array("[", "]"); // Characters to be removed
$gde_date = str_replace($toremove, "", $gde_date); // removed characters from the date string
$gde_time = str_replace($toremove, "", $gde_time); // removed characters from the time string
$gde_date = explode("/", $gde_date);// Seperate date into day, month and year
$gde_time = explode(":", $gde_time);// Seperate time into hour, minute and seconds
// show the arrays for date and time
/*
echo "<pre>";
print_r($gde_date);
print_r($gde_time);
echo "</pre>";
*/
// process the date and time
$logtimestamp = mktime($gde_time[0], $gde_time[1], $gde_time[3], $gde_date[1], $gde_date[2], $gde_date[0]); // Create a timestamp
$created_date = date("F j, Y, g:i a",$logtimestamp); // convert timestamp to date
echo "Log created : ".$created_date; // Show the date/time
$datain = explode("{2{", $gdata); // Process the data
$tableno = 0; // set index for the final array
$finalarr; // Define the array to be populated in the for loop
// So $finalarray[1] would be the first character with all relevant data ie $finalarray[1][name] or $finalarray[1][rank]
for($i = 0; $i < sizeof($datain); ++$i) // for each line in the log
{
if ($datain[$i] != "[N1]=") {
$tableno++;
$datainline = explode(",", $datain[$i]);
for($y = 0; $y < sizeof($datainline); ++$y) // for each Character
{
$datainlinepro = explode("=", $datainline[$y]);
$titlein = substring_between($datainlinepro[0],'"','"');
$titlein = substr($titlein, 0, -1);
if ($titlein == "careerString" || $titlein == "titleString" || $titlein == "name" || $titlein == "note" || $titlein == "onote") {
$valuein = substring_between($datainlinepro[1],'"','"');
} else {
$valuein = str_replace("N", "", $datainlinepro[1]);
}
$finalarr[$tableno][$titlein] = $valuein;
} // End For Loop
} // End if Skip [N1]=
} // End for loop
// Show data
// This is the array that can be used in your own custom applications xD
echo "<pre>";
print_r($finalarr);
echo "</pre>";
} else { // No input to parse - Show input form
// Display Form
?>
<form enctype="multipart/form-data" action="guild_parser.php" method="POST">
<label>
<textarea name="data_in" cols="100" rows="10" id="data_in"></textarea>
</label>
<label>
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</form>
<br />
<?
} // end if ( form parse or form show )
?>Code : Tout sélectionner
<?php
if ($_FILES['gdelog']['error'] == "0" AND $_FILES['gdelog']['tmp_name']) { // Check if the data has been posted
// Function to find the guild data within the log
function substring_between($haystack,$start,$end) {
if (strpos($haystack,$start) === false || strpos($haystack,$end) === false) {
return false;
} else {
$start_position = strpos($haystack,$start)+strlen($start);
$end_position = strpos($haystack,$end);
return substr($haystack,$start_position,$end_position-$start_position);
}
}
// Get time and date of when the log was created
$gdelogfile = file_get_contents($_FILES['gdelog']['tmp_name']);
$gdelogfile = iconv("UCS-2", "UTF-8", $gdelogfile);
$gdelogfile = stristr($gdelogfile, "[");
$gdata = substring_between($gdelogfile,'{1{','}1}'); // Get guild data between the tags and dump the rest of the log
$gdata = stripslashes($gdata);
$gde_date = substr($gdelogfile, 0, 10); // Get date
$gde_time = substr($gdelogfile, 10, 10); // Get time
$toremove = array("[", "]"); // Characters to be removed
$gde_date = str_replace($toremove, "", $gde_date); // removed characters from the date string
$gde_time = str_replace($toremove, "", $gde_time); // removed characters from the time string
$gde_date = explode("/", $gde_date);// Seperate date into day, month and year
$gde_time = explode(":", $gde_time);// Seperate time into hour, minute and seconds
// show the arrays for date and time
/*
echo "<pre>";
print_r($gde_date);
print_r($gde_time);
echo "</pre>";
*/
// process the date and time
$logtimestamp = mktime($gde_time[0], $gde_time[1], $gde_time[2], $gde_date[1], $gde_date[2], $gde_date[0]); // Create a timestamp
$created_date = date("d.m.Y H:i:s", $logtimestamp); // convert timestamp to date
echo "Log created : ".$created_date; // Show the date/time
$datain = explode("{2{", $gdata); // Process the data
$tableno = 0; // set index for the final array
$finalarr; // Define the array to be populated in the for loop
// So $finalarray[1] would be the first character with all relevant data ie $finalarray[1][name] or $finalarray[1][rank]
for($i = 0; $i < sizeof($datain); ++$i) // for each line in the log
{
if ($datain[$i] != "[N1]=") {
$tableno++;
$datainline = explode(",", $datain[$i]);
for($y = 0; $y < sizeof($datainline); ++$y) // for each Character
{
$datainlinepro = explode("=", $datainline[$y]);
$titlein = substring_between($datainlinepro[0],'"','"');
$titlein = substr($titlein, 0, -1);
if ($titlein == "careerString" || $titlein == "titleString" || $titlein == "name" || $titlein == "note" || $titlein == "onote") {
$valuein = substring_between($datainlinepro[1],'"','"');
} else {
$valuein = str_replace("N", "", $datainlinepro[1]);
}
$finalarr[$tableno][$titlein] = $valuein;
} // End For Loop
} // End if Skip [N1]=
} // End for loop
// Show data
// This is the array that can be used in your own custom applications xD
echo "<pre>";
print_r($finalarr);
echo "</pre>";
} else { // No input to parse - Show input form
// Display Form
echo "<html>
<head>
<style type=\"text/css\">
<!--
table { border: 2px solid #C9C9C9; }
td { border: 1px solid #C9C9C9; }
-->
</style>
<body>
<form action=\"update.php\" method=\"post\" enctype=\"multipart/form-data\">
<table border=\"0\" cellspacing=\"0\" cellpadding=\"3\">
<tr>
<td bgcolor=\"#EDEDED\"><b> Example: </b></td>
<td>D:\Games\Warhammer Online - Age of Reckoning\logs\gde.log</td>
</tr>
<tr>
<td bgcolor=\"#EDEDED\"><b> Logfile: </b></td>
<td><input name=\"gdelog\" type=\"file\" size=\"50\" maxlength=\"100000\" accept=\"text/*\"></td>
</tr>
<tr>
<td bgcolor=\"#EDEDED\"> </td>
<td><input type=\"submit\" value=\" Upload \"></td>
</tr>
</table>
</form>
</body>
</html>";
} // end if ( form parse or form show )
?>
je vous remercie d'avance pour votre aide... car j'avoue que là je n'y comprends rien du tout