Jump to content

Reading a text file


Aravinthan

Recommended Posts

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:

 
<?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

 

Link to comment
Share on other sites

  • Replies 100
  • Created
  • Last Reply

Top Posters In This Topic

echo "$parts[0] $parts[1] $parts[2] $parts[3] $parts[4] $parts[5] $parts[6] $parts[7] $parts[8] $parts[9]<BR>";

 

change that to

print_r($parts);

 

and now you dont have to hard code the array values. other than that, im not sure if i understand what you are asking.

Link to comment
Share on other sites

 

 

Oh NVM just realised the difference.

 

So If I understand well,

 

I replace:

$file_handle = fopen("players.txt", "rb");

By this:

$lines = file('myfile.txt');

And this :

while (!feof($file_handle) ) {

By:


while (!feof($lines) ) {

$parts = explode(' ', $line_of_text);

By this:

$lineOneArray =  explode(" ", $lines[1]);

 

And I out put it how?

 

Thanks for your help,

Ara

Link to comment
Share on other sites

Ok so this is what I have:

<?PHP

$lines = file('players.txt');


while (!feof($lines) ) {

$lineOneArray =  explode(" ", $lines[1]);

print_r($lineOneArray);

}

fclose($file_handle);

?>

 

And this is the result:

http://liguedhockeysimule.x10hosting.com/converter/texttosql.php

 

 

Is there something I missed?

 

EDIT:

<?PHP

$lines = file('players.txt');


$lineOneArray =  explode(" ", $lines[1]);

print_r($lineOneArray);



fclose($lines);

?>

 

Changed it to this, and the output:

 

Array ( [0] => [1] => 63 [2] => [3] => 61 [4] => [5] => 54 [6] => [7] => 61 [8] => [9] => 63 [10] => [11] => 60 [12] => [13] => 63 [14] => [15] => 70 [16] => [17] => 66 [18] => [19] => 63 [20] => )

Warning: fclose(): supplied argument is not a valid stream resource in /home/liguehs/public_html/converter/texttosql.php on line 12

.

 

How can I show the other lines? And How can I update a MYSQL table with it?

Link to comment
Share on other sites

output depends on what output you want!

E.g. here's a quick function and example to pull the red data

    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

<?php
$lines = file('myfile.txt');

echo getItem(3,3);
echo "<br>";
echo getItem(6,0);

function getItem($line, $item)
{
global $lines;
$lineOneArray = explode(" ", trim(str_replace("  "," ",$lines[$line])));
return $lineOneArray[$item];
}
?>

Link to comment
Share on other sites

Wow ok thanks,

But wont it be long to give that for all the data I need?

And also there is 3500 datas like this...

Ok I will try to re-explain.

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 1st players data.

What I want is to insert:

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 What I want, lets say line 1 colum 1: 63, I want that to be inserted into a MYSQL table under a colum named C1 for ex, then 61 under C2 colum, and so one. For the lines I dont need: 8,9,10,13,15, 16, 17, 18, 19

I would like to add them in a colum named: line 8, line 9, etc.

 

I dont know if its more clear now...

 

Thanks again for your help,

Ara

Link to comment
Share on other sites

Try to re-explain.

 

    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:

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?

Link to comment
Share on other sites

Yeah but if its for one player, it isnt that much of a deal.

 

But there is 3500 players...

So I would have to give likes 10 000 000:

echo getItem(3,3);

 

Is there a way of putting it in a loop.

 

I was thinking something like, ( in a while loop):

$x= (lets say line) 1;
echo getItem(3,$x);
$x = $x = 20;

 

Would that make sens?

 

 

Thanks for your time and help,

Ara

Link to comment
Share on other sites

I was thinking something like, ( in a while loop):

$x= (lets say line) 1;
echo getItem(3,$x);
$x = $x = 20;

 

Would that make sens?

 

Yes that's exactly it (assuming you mean

$x = $x + 20;

)

As the 3500 is on line 0

Lines 1+$x to 20+$x would be the lines for each player, of course you could create other functions to get details like firstname, lastname etc

Link to comment
Share on other sites

Hi

OK now I go back to feeling dumb lol.

 

I am at decoding line 6, and so far(without the $x):

echo getItem(1,1);
echo getItem(1,2);
echo getItem(1,3);
echo getItem(1,4);
echo getItem(1,5);
echo getItem(1,6);
echo getItem(1,7);
echo getItem(1,;
echo getItem(1,9);
echo getItem(1,10);
echo getItem(2,1);
echo getItem(2,2);
echo getItem(2,3);
echo getItem(2,4);
echo getItem(2,5);
echo getItem(2,6);
echo getItem(2,7);
echo getItem(2,;
echo getItem(2,9);
echo getItem(2,10);
echo getItem(3,1);
echo getItem(3,2);
echo getItem(3,3);
echo getItem(3,4);
echo getItem(3,5);
echo getItem(3,6);
echo getItem(3,7);
echo getItem(3,;
echo getItem(3,9);
echo getItem(4,1);
echo getItem(4,2);
echo getItem(4,3);
echo getItem(4,4);
echo getItem(4,5);
echo getItem(4,6);
echo getItem(5,1);
echo getItem(5,2);
echo getItem(5,3);
echo getItem(5,4);
echo getItem(5,5);
echo getItem(5,6);
echo getItem(6,1);
echo getItem(6,2);
echo getItem(6,3);
echo getItem(6,4);
echo getItem(6,5);
echo getItem(6,6);

 

And I still have 14 lines to go, was wondering if something like this would make more sense:

Idea:

Have a variable(x), that incremets by one on each line.

so that it would go 1 for the 1st one, 2 for the 2nd etc.

And when it reaches 20. It keeps going but, but it restarts from the first getItem.

 

And then you have a 2nd variable (i), that is equal to x, and the i variable, helps us with while loop,

everytime it reaches 20, it restarts....

 

What do you think?

Link to comment
Share on other sites

I think for lines where you use more items then use this getLine function,

$myLine = getLine(1);
echo $myLine[0]." ".$myLine[1]."etc".$myLine[10]."<br>";
function getLine($line)
{
   global $lines;
   return explode(" ", trim(str_replace("  "," ",$lines[$line])));
}

//other extended option
$player = getPlayer();
echo $player['Name'];
echo $player['num1'][0];
echo $player['num1'][1];
echo $player['num1'][2];

$tmpLine = 0;
function getPlayer()
{
   global $tmpLine;
   //Setup details
   $details = array();
   $details['num1'] = getLine($tmpLine+1);
   $details['Name'] = $lines[$tmpLine+14];
   
   $tmpLine = $tmpLine+20;
   return $details;
}


 

EDIT: sorry had a visitor

 

Building a few functions to sort out your data does helps the function above gives am example of this, so you can see its building an array that would hold details about the play and updates the counter for the next player, you could pass a var and times that by 20 instead of adding up,

ie

function getPlayer($player)
{
   $player--;
   $player = $player * 20
   //Setup details
   $details = array();
   $details['num1'] = getLine($player+1);
   $details['Name'] = $lines[$player+14];
   
   return $details;
}
$player= getPlayer(1);
echo $player['Name'];

$player= getPlayer(2);
echo $player['Name'];

 

With this in mind you could clean up the data your pulling and end up with an array, use that, then get the next players details..

 

I hope what i am suggestion is making sense!

Link to comment
Share on other sites

Okay, assuming each player DOES have 20 lines,

then you can use the second function, what you need do it extract the data (you know the data better than I do)

 

$player = 1;
while($details = getPlayer($player)) //loop until false
{
    var_dump($details); //dump data // change to echo data correctly
    $player++; //edit: oops need this line!
}

function getPlayer($player)
{
   $player--;
   $player = $player * 20
   //Check first line
   if(empty($lines[$player+1])) return false;
   //Setup details
   $details = array();
   $details['num1'] = getLine($player+1);
//Add other line extractions (2-13)
   $details['Name'] = $lines[$player+14]; //Milan Hnilicka
   $details['Blank'] = $lines[$player+15]; //?
   $details['drafted'] = $lines[$player+16]; //drafted
   return $details;
}

 

please note this is all typed on the fly and probably had bugs

Edit you may need a function for lines like "1.16 (24.3.2002) "

Link to comment
Share on other sites

Hi,

 

Ok so this is the code:

<?php
$lines = file('players.txt');

$myLine = getLine(1);
echo $myLine[0]." ".$myLine[1]."etc".$myLine[10]."<br>";
function getLine($line)
{
   global $lines;
   return explode(" ", trim(str_replace("  "," ",$lines[$line])));
}


//other extended option
$player = getPlayer();
echo $player['Name'];
echo $player['num1'][0];
echo $player['num1'][1];
echo $player['num1'][2];

$tmpLine = 0;
$player = 1;
while($details = getPlayer($player)) //loop until false
{
    var_dump($details); //dump data // change to echo data correctly
    $player++; //edit: oops need this line!
}

function getPlayer($player)
{
   $player--;
   $player = $player * 20
   //Check first line
   if(empty($lines[$player+1])){
   return false;
   }
   //Setup details
   $details = array();
   $details['num1'] = getLine($player+1);
//Add other line extractions (2-13)
   $details['Name'] = $lines[$player+14]; //Milan Hnilicka
   $details['Blank'] = $lines[$player+15]; //?
   $details['drafted'] = $lines[$player+16]; //drafted
   return $details;
}

?>

 

And i get this error:

Parse error: syntax error, unexpected T_IF in /home/liguehs/public_html/converter/texttosql.php on line 33

 

And line 33 is:

if(empty($lines[$player+1]))return false;

 

I tried:

   if(empty($lines[$player+1])){
   return false;}
   

 

  But same error.

 

 

  Once again thanks for all your help,

  Ara

 

Link to comment
Share on other sites

few typo's

try this

 

<?php
$lines = file('players.txt');

//$myLine = getLine(1);
//echo $myLine[0]." ".$myLine[1]."etc".$myLine[10]."<br>";

function getLine($line)
{
   global $lines;
   return explode(" ", trim(str_replace("  "," ",$lines[$line])));
}


//other extended option
/*
$player = getPlayer();
echo $player['Name'];
echo $player['num1'][0];
echo $player['num1'][1];
echo $player['num1'][2];
*/

$tmpLine = 0;
$player = 1;
echo "<pre>";
while($details = getPlayer($player)) //loop until false
{
var_dump($details); //dump data // change to echo data correctly
$player++; //edit: oops need this line!
}

function getPlayer($player)
{
global $lines;
$player--;
$player = $player * 20;
//Check first line
if(empty($lines[$player+1])){
	return false;
}
//Setup details
$details = array();
$details['num1'] = getLine($player+1);
//Add other line extractions (2-13)
$details['Name'] = $lines[$player+14]; //Milan Hnilicka
$details['Blank'] = $lines[$player+15]; //?
$details['drafted'] = $lines[$player+16]; //drafted
return $details;
}

?>

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.