Forum Moderators: coopster

Message Too Old, No Replies

Parsing data from text files with multiple words

just need modificatons..

         

xpiamchris

1:23 am on Feb 18, 2009 (gmt 0)

10+ Year Member



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)

$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 DateEnd DateUPCISRCVendor IdentifierQuantityPartner ShareExtended Partner SharePartner Share CurrencySale Or ReturnApple IdentifierArtist/Show/DeveloperTitleLabel/Studio/NetworkSideProduct Type IdentifierAccount IdentifierCountry Of SalePre-order FlagPrepaidPrepaid TypeISAN/Other IdentifierCMA FlagCustomer PriceCustomer Currency
11/30/200812/27/2008USS6Y0800001USS6Y0800001 4 0.70 2.80USDS295876306Monkey Pirates4:18Monkey Pirates Music1H80065888USA 0.99USD

Total 2.80USD

Thanks in advance

[edited by: eelixduppy at 2:47 am (utc) on Feb. 18, 2009]
[edit reason] side scroll [/edit]

coopster

2:37 pm on Feb 18, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Parse on the tabs instead? That would seem appropriate to me anyway. Consider fgetcsv [php.net] and one other thing, even though you may think you know what data is going to be present and where it came from, it is still wise to escape it before INSERTing into your database table. I recommend using mysql_real_escape_string [php.net].

tbarbedo

2:52 pm on Feb 18, 2009 (gmt 0)

10+ Year Member



Yeah I believe parsing it by tabs would be what you are looking for.

$content = explode("\t", $FileContent);

Then just loop through each value in the array...