Jump to content

Parsing CSV files


piznac

Recommended Posts

Ok,. this may be a bit broad. But hopefully someone can point me in the right direction. I have a function to parse a csv file and insert into a table in my database. This works great so long as its a very simple file.

<?php

function quickbookimport ($filenameb){
	if (file_exists($filenameb)){
     
     $handle = fopen("$filenameb", "r") or die("no file");
     while (($data = fgetcsv($handle, 50000, "," )) !== FALSE ){
    $or_d = explode("/", $data[0]);
    $or_d2 = "$or_d[2]-$or_d[0]-$or_d[1]";
   
      $sh_d = explode("/", $data[4]);
    $sh_d2 = "$sh_d[2]-$sh_d[0]-$sh_d[1]";
   
$import="INSERT INTO orders(`or_date`,`ship_date`,`ship_via`,`job_name`,`job_desc`,`finishing`,`or_num`,`po_num`,`cust_name`,`tracking`,`pro_date`) values('$or_d2','$sh_d2','$data[5]','$data[6]','$data[7]','$data[8]','$data[1]','$data[2]','$data[3]','','')
ON DUPLICATE KEY UPDATE or_date = values(or_date), ship_date = values(ship_date), ship_via = values(ship_via), job_name = values(job_name), job_desc = values(job_desc), finishing = values(finishing), po_num = values(po_num), cust_name = values(cust_name), tracking = values(tracking), pro_date = values(pro_date)";
  mysql_query($import) or die("insert" . mysql_error());
}
fclose($handle);
     echo "Import Done - You will be redirected in 5 seconds";
     
    echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"5; URL=http://sewnice.biz/control/\">";
   }else{
   echo "Import Unsuccessful";
   }
}
?>

 

 

Now suppose I had a csv file that read something like this:

 

2007-05-20,16:24:25,"{WRL}Server/Recruiting",cor1g_con,CON,0,10

"{WRL}Elite.BIG{",Reb,-1,1,4,155,71.222.7.68,399a0fdd1b258a573c435ccc1cc03edf

"{WRL}gen.Kinzu",Emp,20,15,1,121,76.25.37.241,b03e4083c2a20010760f6317beb0ff7d

"{WRL}fm.=^UW^=M",Reb,5,5,2,54,66.108.170.74,e1c1e880f8130d9748a46368d4339fdf

"{WRL}.Lt.Matt",Emp,0,0,0,149,207.162.155.166,8f8fde5b0a5305d7b5281051c91adc79

2007-05-20,16:30:59,"{WRL}Server/Recruiting",dea1g_con,CON,-1,93

"{WRL}Elite.BIG{",Reb,6,5,5,159,71.222.7.68,399a0fdd1b258a573c435ccc1cc03edf

"{WRL}fm.=^UW^=M",Reb,36,17,4,59,66.108.170.74,e1c1e880f8130d9748a46368d4339fdf

 

The items in bold I dont need,.. but I know the will impede my parsing of this file. Is there a way to strip these out? Or bypass them someone how? BTW there is no pattern to the number of lines that may occur between each of these in bold. This is an export form a game server and each line is a different player. So it could be anywhere from 1 to 16 lines between each "bold" line (which is the description of the actual game. I am only interested in the first two or three fields.

 

Im not looking for a script (although if you  have one handy,. it would save me some work :)) just point me in the right direction...thanks!

Link to comment
https://forums.phpfreaks.com/topic/64147-parsing-csv-files/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.