sasi Posted March 29, 2007 Share Posted March 29, 2007 yesterday the switch case worked properly bu after this another problem is that a special charcater like this "</br>" is also appended at last of the each line in the destination txt file,and i have to avoid this bcoz i have to insert that data into the database with load command,so please once execute this and help me as early as possible. Main goal of this is i am converting the unix date format(log file) into the MySql date format.(Ex : "Tue Oct 13 10:11:01 2007 3 192.168.1.13 31533058 /home/localftp1/TADSIM-Beta-3.0_withDB.exe b _ i r localftp1 ftp 0 * c" into like this: "2007:02:14 and remainig txt also"). <html> <body> <?php $file = fopen("vsftpd.log","r+")or exit("Unable to open file!"); $file1 = fopen("result.txt","w+")or exit("Unable to open file!"); //echo "file opened"."</br>"; while(!feof($file))//Read the entire file up to the EOF. { $date = array();//Array to store the entire data of the file $date= fgets($file)."</br>"; $month = substr($date,4,3); $rstring = substr($date,25); $result = trim($rstring); $fitting = strstr($result,</br>); echo $fitting; switch($month)//Switch case to check and print the month in the number format { case "Jan": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "01" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Feb": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "02" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Mar": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "03" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Apr": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "04" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "May": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "05" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Jun": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "06" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Jul": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "07" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Aug": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "08" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Sep": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "09" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Oct": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "10" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Nov": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "11" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; case "Dec": $a = sprintf("%2s:%2s:%2s",substr($date,20,4), "12" ,substr($date,8,2)); fwrite($file1,$a); fwrite($file1," "); fwrite($file1,$result); //fwrite($file1,$fitting); break; }//End of the switch case. //echo "\t".$rstring."</br>"; fwrite($file1," "); //$len=strlen($result); //echo $len."</br>"; //echo $result[$len:7]; //$result = ($result[$len:7] = ""); //fwrite($file1,$result); //substr_replace($file1,"</br>"," "); //fwrite($file1,"\n"); }//End of the while loop. fclose($file);//File is closed. ?> <?php $uname = "root"; $pwd = ""; $db = "mic"; $sys = "localhost"; mysql_connect($sys,$uname,$pwd); mysql_select_db($db)or die("unable to connect"); echo "Database connected"; $query = "LOAD DATA LOCAL INFILE 'C:/Program Files/EasyPHP1-8/www/mic/result.log' REPLACE INTO TABLE `logging` FIELDS TERMINATED BY ' ' LINES TERMINATED BY '\n'"; $value = mysql_query($query); echo "Query executed"; mysql_close(); ?> </body> </html> Link to comment https://forums.phpfreaks.com/topic/44738-i-had-a-problem-when-writing-into-a-file-a-special-character-is-also-appending/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.