Regexp

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
jkmcgrath
Forum Commoner
Posts: 33
Joined: Mon May 05, 2003 10:07 am

Regexp

Post by jkmcgrath »

Hi,

I am trying to remove the coma between a first and last name and then place the first name in a string and the last name in a string. I keep failing no matter how I try it. I am parsing this out of a text file also.

The value of the $fullname would be like McGrath, John

I need that to be $lastname = McGrath $firstname = John

Does this make any sense? Havent sleep for days over this file parsing stuff. A real pain 8O

Code: Select all

$fullname = fread($fp,25);
$fullname = trim($fullname);

echo "name",$fullname;
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Try this out for size

Code: Select all

($fp = fopen("path_to_your_text_file", 'r')) or die ("Couldn't open the data file"); 
    
// Iterate through each line of the file 
while ( ! feof($fp) ) { 
   $line = fgets($fp, 1024); 
       
   // remove any extra white space or carriage returns 
   $line = chop($line); 
       
   // spilt each line into individual fields 
   $names = explode(",", $line); 

   // print the first name
   print $names[1];

   // print the last name
   print $names[0];
    
}
Mark
jkmcgrath
Forum Commoner
Posts: 33
Joined: Mon May 05, 2003 10:07 am

Post by jkmcgrath »

Thanks man!!!!!!!!!!!!

THat worked like a champ!

is this what removed the , and made it 2 strings?

Code: Select all

// spilt each line into individual fields 
   $names = explode(",", $line);
Let me ask you this question now. This seems like it can save me some typing on the rest of this.

Here is what I have.

Code: Select all

; 1999 Week 17 99WK17QO.NFL  QSS Offensive Player Stats
;                                                  |--------  Passing  --------|-- Rushing --|- Receiving -|-   Kicking      FG Distances -|
;                                                                    2-                    2-            2-                  01  30  40
;Name                     ID #  Tm/ID  Pos I S P T Cmp Att Yds In TD Pt Sck/Yds Car Yds TD Pt Rec Yds TD Pt EPM EPA FGM FGA -29 -39 -49 50+ FL TDs by distance
Aguiar, Louie             3024 GB  9021 P    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 
Aikman, Troy                 2 DAL 9014 QB   1 1 0  23  32 288  0  2  0   0   0   3  -2  0  0   0   0  0  0   0   0   0   0   0   0   0   0  0 T004T090
Akers, David              1594 PHI 9016 PK   0 1 0   0   0   0  0  0  0   0   0   0   0  0  0   0   0  0  0   0   0   1   1   0   0   1   0  0 G046
Alexander, Derrick        1020 KC  9010 WR   1 1 0   0   0   0  0  0  0   0   0   0   0  0  0   1  18  0  0   0   0   0   0   0   0   0   0  0 
Alexander, Stephen        1504 WAS 9018 TE   1 1 0   0   0   0  0  0  0   0   0   0   0  0  0   2  38  0  0   0   0   0   0   0   0   0   0  0 
Alford, Brian             1513 NYG 9015 WR   0 1 0   0   0   0  0  0  0   0   0   0   0  0  0   1   7  1  0   0   0   0   0   0   0   0   0  0 C007
This is how I am parsing this out getting ready to put it in a DB.

Code: Select all

$offPlayers = "./nfl.txt" ;
if (!($fp = fopen($offPlayers, "r"))) die ("Cannot open file");
$player = fgets($fp);
$player = fgets($fp);
$player = fgets($fp);
$player = fgets($fp);
do
{
$fullname = fread($fp,25);
$fullname = trim($fullname);
echo "name",$fullname;
$offset = fgetc($fp);
$id = fread($fp, 4);
$id = trim($id);
echo "ID",$id;
$offset = fgetc($fp);
$team = fread($fp, 3);
$team = trim($team);
echo "Team",$team;
$offset = fgetc($fp);
$teamid = fread($fp, 4);
$teamid = trim($teamid);
echo "Team ID",$teamid;
$offset = fgetc($fp);
$pos = fread($fp, 2);
$pos = trim($pos);
echo "Posistion",$pos;
$offset = fgetc($fp);
$inj_stat = fread($fp, 1);
$inj_stat = trim($inj_stat);
echo "Injury",$inj_stat;
$offset = fgetc($fp);
$start = fread($fp, 1);
$start = trim($start);
echo "Started",$start;
$offset = fgetc($fp);
$played = fread($fp, 1);
$played = trim($played);
echo "Played",$played;
$offset = fgetc($fp);
$inact = fread($fp, 1);
$inact = trim($inact);
echo "Inactive",$inact;
$offset = fgetc($fp);
$pcmp = fread($fp, 3);
$pcmp = trim($pcmp);
echo "Completions",$pcmp;
$offset = fgetc($fp);
$patt = fread($fp, 3);
$patt = trim($patt);
echo "Attempts",$patt;
$offset = fgetc($fp);
$pyds = fread($fp, 3);
$pyds = trim($pyds);
echo "Passing Yards",$pyds;
$offset = fgetc($fp);
$pint = fread($fp, 2);
$pint = trim($pint);
echo "Interceptions",$pint;
$offset = fgetc($fp);
$ptds = fread($fp, 2);
$ptds = trim($ptds);
echo "TDS",$ptds;
$offset = fgetc($fp);
$p2pt = fread($fp, 2);
$p2pt = trim($p2pt);
echo "2points",$p2pt;
$offset = fgetc($fp);
$psck = fread($fp, 3);
$psck = trim($psck);
echo "Sacked",$psck;
$offset = fgetc($fp);
$psckyds = fread($fp, 3);
$psckyds = trim($psckyds);
echo "YDS",$psckyds;
$offset = fgetc($fp);
$car = fread($fp, 3);
$car = trim($car);
echo "carries",$car;
$offset = fgetc($fp);
$ryds = fread($fp, 3);
$ryds = trim($ryds);
echo "rushing yds",$ryds;
$offset = fgetc($fp);
$rtds = fread($fp, 2);
$rtds = trim($rtds);
echo "rushing tds",$rtds;
$offset = fgetc($fp);
$r2pt = fread($fp, 2);
$r2pt = trim($r2pt);
echo "Rush 2pts",$r2pt;
$offset = fgetc($fp);
$rec = fread($fp, 3);
$rec = trim($rec);
echo "Receptions",$rec;
$offset = fgetc($fp);
$recyds = fread($fp, 3);
$recyds = trim($recyds);
echo "Reception yds" , $recyds;
$offset = fgetc($fp);
$rectds = fread($fp, 2);
$rectds = trim($rectds);
echo "Reception tds" , $rectds;
$query = "INSERT INTO $dbtable (Name) VALUES ('$fullname')";
$result = mysql_query($query);
$player = fgets($fp);
echo "<br>";
&#125;
while(!Feof($fp));
fclose($fp);
That is a whole lot of typing. Is there a way I can do the same thing with what you just did? One colum has no number or letters in it so I am not sure how that would work either.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

For an explanation of explode, look here http://uk.php.net/manual/en/function.explode.php

The explode function wont be any use to in what you are trying to achieve.

Not sure of the best way of going about it.
jkmcgrath
Forum Commoner
Posts: 33
Joined: Mon May 05, 2003 10:07 am

Post by jkmcgrath »

Ok Mark thanks that gets me over the hump and the rest is just typing. Thanks again.
Post Reply