Jump to content

Looking for some help parsing a flat file.


Recommended Posts

I have a flat file that which is in this format like this

 

1038140, Hail of Arrows, 1, Long, Wenchratt, 25
1038144, Razorfoot Griffin, 1, Very Long, Pezfreak, 5
1038145, Razorfoot Griffin, 1, Very Long, Pezfreak, 5
1038150, Samite Healer, 1, Very Long, Pezfreak, 8
1038152, White Knight, 1, Very Long, Pezfreak, 7
1038153, White Knight, 1, Very Long, Pezfreak, 7
1038154, Boggart Harbinger, 1, Long, Pezfreak, 14
1038159, Rancor, 2, Long, Pezfreak, 12
1038163, Scattershot Archer, 1, Very Long, Pezfreak, 6
1038166, Angelic Avatar, 1, Long, Grunfell, 180

 

And I'm trying to parse this data so it can be inserted into my database which is basically the same format except I have 2 extra columns

Here are the columns

ID---Card_Name--Amount--Duration--Seller--Cost--"Price_Per"--"Date_Found"

 

The column Price_Per is the Cost/Amount and the date is pretty simple the date.

 

I have for the last 6 months just been taking this flat file putting it Excel and then text to columns it and add the other 2 columns of data.

It only takes a few minutes but I'd like to have a way to do on my site with a form upload, parse and insert the data.

 

I have done a lot with php but by no means am I anywhere near an expert and never used the file functions to any great degree.

 

I'm thinking I need to put this in an array somehow.. explode it on the " , " then somehow add in the Price_Per function and just add the CURRENT_TIMESTAMP into the sql insert which I must loop through each line?

 

Little baffled here so any help on dealing with inserting data, tips examples anything would be helpful.

 

Thanks!

 

 

Link to comment
Share on other sites

Humm,

 

So in this I ran into another issue..

 

One of my cards has a comma in the name so I needed to strip that out first.

 

So I figured this would do it..

	$replace = fopen("trades.dat", 'w+');
str_replace("Eladamri, Lord of Leaves","Eladamri Lord of Leaves",'$replace');
fclose('trades.dat');

 

What actually happened was the file was dumped.

 

Any suggestions on this one?

Link to comment
Share on other sites

Can you re-generate the csv file so it's in a proper format?  If you have fields that have a comma in them they should be surrounded by quotes so that when the file is parsed the comma is treated as part of the string rather than as a field separator.

 

Otherwise you can fix it by using file_get_contents, str_replace then file_put_contents.

 

Link to comment
Share on other sites

Unfortunately I have no control over the output of the flat file its from a 3rd party source.

 

I've been working on using this function to do a replace...

$str=implode("\n",file('trades.dat'));
$fp=fopen('trades.dat','a');
//replace something in the file string
$str=str_replace('Eladamri, Lord of Leaves','Eladamri Lord of Leaves',$str);
//now, rewrite the file
fwrite($fp,$str);
print_r($str);
fclose($fp);

 

Which works, but if I refresh the page it actually duplicates the data... still working on this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.