Parsing data from text files with multiple words

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xpiamchris
Forum Newbie
Posts: 10
Joined: Thu Jan 15, 2009 4:17 pm

Parsing data from text files with multiple words

Post by xpiamchris »

Hey guys,

If anyone can answer this, I would really appreciate it. I am trying to parse a txt file to put into a mysql database. Currently, I have it so that all spaces are taken out so that I can explode it properly and insert it into the mysql table.. but what if i wanted to keep the spaces in the data fields?

Here is my code (it works, just eliminates all spaces)

Code: Select all

$fp = fopen($filename,"r") or die("Unable to open $filename");
 
 //parsing info
 $count=0;
 $sdate="";
 $edate="";
 $upc="";
 $isrc="";
 $venid="";
 $quantity="";
 $pshare="";
 $epshare="";
 $pscurrency="";
 $sorr="";
 $appleid="";
 $artist="";
 $title="";
 $label="";
 $side="";
 $ptid="";
 $accountid="";
 $country="";
 $poflag="";
 $prepaid="";
 $ptype="";
 $isan="";
 $cmaflag="";
 $custprice="";
 $custcurrency="";
 
 
 
 while(!feof($fp)) {
 $string = fgets($fp, 1024);
 $string2 = str_replace(" ","",$string);
 $count = count(explode(" ",$string2));
 $xp=explode("  ",$string2);
 $sdate = $xp[0];
 list($m, $d, $y) = preg_split('/\//', $sdate);
 $sdate = sprintf('%4d%02d%02d', $y, $m, $d);
 
 $edate = $xp[1];
  list($m2, $d2, $y2) = preg_split('/\//', $edate);
 $edate = sprintf('%4d%02d%02d', $y2, $m2, $d2);
 
 $upc = $xp[2];
 $isrc = $xp[3];
 $venid = $xp[4];
 $quantity = $xp[5];
 $pshare = $xp[6];
 $epshare = $xp[7];
 $pscurrency = $xp[8];
 $sorr = $xp[9];
 $appleid = $xp[10];
 $artist = $xp[11];
 $title = $xp[12];
 $label = $xp[13];
 $side = $xp[14];
 $ptid = $xp[15];
 $accountid = $xp[16];
 $country = $xp[17];
 $poflag = $xp[18];
 $prepaid = $xp[19];
 $ptype = $xp[20];
 $isan = $xp[21];
 $cmaflag = $xp[22];
 $custprice = $xp[23];
 $custcurrency = $xp[24];
 
 $query = "INSERT INTO salesreport (s_date, e_date, upc, isrc, ven_id, quantity, p_share, ep_share, ps_currency, sorr, apple_id, artist, title, label, side, pt_id, account_id, country, po_flag, prepaid, p_type, isan, cma_flag, cust_price, cust_currency) VALUES ('$sdate','$edate','$upc','$isrc','$venid','$quantity','$pshare','$epshare','$pscurrency','$sorr','$appleid','$artist','$title','$label','$side','$ptid','$accountid','$country','$poflag','$prepaid','$ptype','$isan','$cmaflag','$custprice','$custcurrency')";
 
 $result = mysql_query($query);
 
 }
And here is the data I'm given.. the data is tab delimited
Start Date End Date UPC ISRC Vendor Identifier Quantity Partner Share Extended Partner Share Partner Share Currency Sale Or Return Apple Identifier Artist/Show/Developer Title Label/Studio/Network Side Product Type Identifier Account Identifier Country Of Sale Pre-order Flag Prepaid Prepaid Type ISAN/Other Identifier CMA Flag Customer Price Customer Currency
11/30/2008 12/27/2008 USS6Y0800001 USS6Y0800001 4 0.70 2.80 USD S 295876306 Monkey Pirates 4:18 Monkey Pirates Music 1 H 80065888 USA 0.99 USD

Total 2.80 USD
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Parsing data from text files with multiple words

Post by requinix »

Did you know that fgetcsv can use a tab character as a delimiter when reading from a file?
Post Reply