Page 1 of 1

Reading,Formatting a text file

Posted: Sun Aug 09, 2009 12:55 pm
by Aravinthan
Hi guys,

Ok so first of all here is my problem:

I would like to read a text file and then format it to a SQL type of format.

So I tought, lets start it easy. What I am trying is to take a line and insert it into an array. Then output the array.
Simple right? Thats what I tought until, I realised the lines dont have the same number of datas. Some has 10, some has 5.

And there is also some lines that I need to split up a part and some that can stay toghter.

Well, I'll show you guys the code:

Code: Select all

 
<?PHP
 
$file_handle = fopen("players.txt", "rb");
 
while (!feof($file_handle) ) {
 
$line_of_text = fgets($file_handle);
$parts = explode(' ', $line_of_text);
echo "$parts[0] $parts[1] $parts[2] $parts[3] $parts[4] $parts[5] $parts[6] $parts[7] $parts[8] $parts[9]<BR>";
 
}
 
fclose($file_handle);
 
?>
 
Only the first 5 gets shown.

I will attach the text file that I would like to format.
But here is a sample of it:
3500
63 61 54 61 63 60 63 70 66 63
63 71 68 61 115 42 10 0 1 4 1
1973 24 6 950000 0 1991 4 17 98
0 0 0 0
0 0 0 0
179000 3000 908 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 4 194 7 3
0 0 0 0 0
-
Milan Hnilicka

drafted
150080070070075095144103068062125080125
1.16 (24.3.2002)
1.16 (24.3.2002)
0 0 98 65 70

This is the first player.

The 3500 at the top, is just to say that there is 3500 players, but it doenst matter too much.

Ok so here is the details:
The first 7 lines, I need to split them apart, each numbers = different strings = different colum name for the Database.
The lines 8,9 and 10 dont need it.
11 and 12 same as the first 7 ones.
Dont need 13, but need 14.
Dont need 15, 16, 17, 18, 19
But the line 20 need it like the first 7 ones.

But I would need to insert the lines I dont need anyways, as after that I would like to export the MYSQL data back into this format.

So can some one help me on doing this?

I am not sure if I am clear... If I aint please dont hesistate to ask more information...

Thanks for your help and Time,
Ara

Re: Reading,Formatting a text file

Posted: Tue Aug 11, 2009 8:54 am
by Aravinthan
This is what the first players data:
63 61 54 61 63 60 63 70 66 63
63 71 68 61 115 42 10 0 1 4 1
1973 24 6 950000 0 1991 4 17 98
0 0 0 0
0 0 0 0
179000 3000 908 0 0 0
1 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 4 194 7 3
0 0 0 0 0
-
Milan Hnilicka

drafted
150080070070075095144103068062125080125
1.16 (24.3.2002)
1.16 (24.3.2002)
0 0 98 65 70
And I want it to be formatted to something like this:

Code: Select all

 
Update( SET name = 'Milan Hnilicka', shooting = '63', playmaking = '61', stickhandling = '54', checking = '61', marking = '63', 
hitting = '60', skating = '63', endurance = '70', penalty = '66', faceoffs = '63', leadership = '63', strength = '71', 
potentiel = '68', consistency = '61', greed = '115',fighting = '42', click = '10', team='0', main_position='1', country='4', 
handed='1', birth_year='1973', birth_day='24', birth_month='6', salary='950000', contract_lenght='0', draft_year='1991',
draft_round='4', drafted_by='17', rights='98', week_goals=0', week_assists='0', week_gwg='0', month_goals='0', month_assists='0', 
month_gwg='0', record_goals='179000', record_assists='3000', record_points='908', no_trade_switch='0', two-way_switch='0',
player-team_option='0', status='1', rookie='0', considering_offer_data='0', team_offering='0', amount_time_spent_considering='0',
injury='0', line8='0  0  0  0  0  0  0  0  0  0', line9='0  0  0  0  0  0  0  0  0  0', line10='0  0  0  0  0  0  0  0  0  0',
goal_streak='0', point_streak='0', total_gp='0', suspended_game='0', training='0', weight='194', height='7', status_in_organization='3',
best_streak_games='0', best_streak_gwg='0', best_streak_assists='0', best_streak_points='0', best_streak_goals='0', line13='-',
line15='/n', drafted='drafted='drafted', line17='150080070070075095144103068062125080125', line18='1.16 (24.3.2002)', line19='1.16 (24.3.2002)',
attitude='0', alternate_position='0', nhl_rights='98', injury_prone='65', overral_draft='70' Where name='$name');
 
I think I didnt write the correct Update code, but you get the point... I hope.

And I need this to be the same thing for all the players. I guess we can do that with a while loop, no?