I'm not really accustomed to string manipulation and so I was wondering
if any of you could be any help i speeding up this script intended to
change the format of some saved log information into a CSV file while
removing duplicate records.
The main problem is that the script currently takes about 20 seconds to
execute, and were it to take much longer it would time out.
Below is the script itself, and then some example lines from the log
file it processes:
<?
$data ="";
$fp = fopen("logs.txt ", "r");
while(!feof($fp )){
$data .= fread($fp, 4096);
}
fclose($fp);
$fullArray = explode("\n", $data);
$ArrayofArrays[0] = array("","");
$myArray[0]=$fullArray[0];
(int)$flg;
for($i=0;$i<cou nt($fullArray); $i++){
$flg=0;
for($j=0;$j<cou nt($myArray);$j ++){
if($myArray[$j]==$fullArray[$i]){$flg++;}
}
if($flg==0){
$myArray[count($myArray)]=$fullArray[$i];
}
}
for($maincount= 0;$maincount<co unt($myArray);$ maincount++){
$newArray = explode("\"",$m yArray[$maincount]);
$newArray[0] = str_replace(arr ay("[","]","+"),"", $newArray[0]);
$outArray = explode(" ", $newArray[0]);
$tmpArray = explode(" ", $newArray[1]);
$j=count($outAr ray);
for($i=$j;$i<$j +count($tmpArra y);$i++){
$outArray[$i] = $tmpArray[$i-$j];
}
$tmpArray = explode(" ", $newArray[2]);
$j=count($outAr ray);
for($i=$j;$i<$j +count($tmpArra y);$i++){
$outArray[$i] = $tmpArray[$i-$j];
}
$outArray[count($outArray )] = $newArray[3];
$outArray[count($outArray )] = $newArray[5];
trim_array($out Array, " \n\t:;,");
$ArrayofArrays[$maincount]=$outArray;
}
$out = fopen("output.c sv", "a");
for($i=0;$i<cou nt($ArrayofArra ys);$i++){
for($j=0;$j<cou nt($ArrayofArra ys[$i]);$j++){
if($ArrayofArra ys[$i][$j]!=""){
fwrite($out,$Ar rayofArrays[$i][$j]);
fwrite($out,"," );
}
}
fwrite($out,"\n ");
}
fclose($out);
//printout($Final AofAs);
function printout($a){
echo "<br><br>";
for($i=0;$i<cou nt($a);$i++){
if(count($a[$i])!=1 && count($a[$i])!=0){printout( $a[$i]);}
else{
if($a[$i]!="" && $a[$i]!="-"){
echo $a[$i];
echo "<br>";
}
}
}
}
function trim_array($a /*array to be trimmed*/,$b /*string of chars to
be removed*/){
for($i=0;$i<cou nt($a);$i++){
$a[$i]=trim($a[$i],$b);
}
}
?>
jpgme.co.uk: [25/May/2006:13:04:47 +0100] 81.157.187.150 - - "GET /
HTTP/1.1" 200 5736 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:47 +0100] 81.157.187.150 - - "GET /
HTTP/1.1" 200 5736 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT
5.1; SV1; .NET CLR 1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:47 +0100] 81.157.187.150 - - "GET
/images/title.gif HTTP/1.1" 200 5237 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/but1.gif HTTP/1.1" 200 696 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/vline.gif HTTP/1.1" 200 85 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/but2.gif HTTP/1.1" 200 742 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/but3.gif HTTP/1.1" 200 742 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/but4.gif HTTP/1.1" 200 506 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/but5.gif HTTP/1.1" 200 711 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/but6.gif HTTP/1.1" 200 600 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/ind_th2.jpg HTTP/1.1" 200 29533
"http://www.martinsphot os.co.uk/" "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; .NET CLR 1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:48 +0100] 81.157.187.150 - - "GET
/images/ind_th1.jpg HTTP/1.1" 200 18673
"http://www.martinsphot os.co.uk/" "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; .NET CLR 1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:49 +0100] 81.157.187.150 - - "GET
/images/ind_th3.jpg HTTP/1.1" 200 9298
"http://www.martinsphot os.co.uk/" "Mozilla/4.0 (compatible; MSIE 6.0;
Windows NT 5.1; SV1; .NET CLR 1.1.4322)"
jpgme.co.uk: [25/May/2006:13:04:54 +0100] 81.157.187.150 - - "GET
/gallery.php HTTP/1.1" 200 5787 "http://www.martinsphot os.co.uk/"
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
1.1.4322)"