John,
I reread your post, and spent a bit of time to rewrite my last post.  This
should be more appropriate for your needs.  you'll need to add a few lines
yourself to complete it.  Let me know if it works.
hugh
<?php

// get info from text file and put it in a mysql table
// run it once only, otherwise you'll get duplicate records

$cards=  ;  // put the number of cards in the text file here
   // if you have empty lines between each card, you need to increase the
value of $cards to twice or three times the number of actual records
depending on the number of blank lines between records.

// input the database vars
$db=" ";
$table=" ";
$user=" ";
$pass=" ";

$fp=fopen("./directory/file.txt","r") or die ("Couldn't open file: "); //
the path and name of your text file here
$link=mysql_connect("localhost","$user","$pass");
if (! $link) die("couldn't connect to mysql");
mysql_select_db($db,$link) or die ("couldn't open $db ".mysql_error());

for ($i=0;$i<$cards;$i++)
 {
 if (fgets($fp,100)=="") continue; // fgets increments each time it's
called.
 if (fgets($fp,100)==" ") continue; // sometime you need this too.
 $temp=fgets($fp,100);
 $card_name=trim(preg_replace("Card Name:","",$temp));  // preg_replace gets
rid of the extra text, and trim gets rid of any spaces before and after the
part you want.
 echo $card_name."<br>";
 $temp=fgets($fp,100);
 $card_color=trim(preg_replace("Card Color:","",$temp));
 echo $card_color."<br>";
 $temp=fgets($fp,100);
 $mana_cost=trim(preg_replace("Mana Cost:","",$temp));
 echo $mana_cost."<br>;

 // you can complete the rest of this yourself.  Also, increase the limit of
characters for your card_text and flavor_text

 $query="insert into $table (card_name, card_color, mana_cost,
type_and_class, pow_tou, card_text, flavor_text, artist, rarity,
card_number) values ('".addslashes($card_name)."',
'".addslashes($card_color)."', '".addslashes($mana_cost)."', ...you can fill
in the rest yourself....) ";
 $result=mysql_query($query);
 if (!$result) die ("failed on insert ".mysql_error());
 }
mysql_close($link);
fclose($fp);
?>
----- Original Message -----
From: "John Wulff" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, March 07, 2003 5:25 PM
Subject: [PHP] Mining a file for data


> I'm just learning PHP so please excuse my ignorance.  I'm trying to
extract
> the data from a simply formatted text file via a simple PHP script.  The
> goal of this project is to take Magic the Gathering spoiler lists and dump
> them into a MySQL database.  I've got plenty of experience with MySQL but
> not file parsing.  How do I about parsing this data?  What sort of pattern
> recognition do I use (if that's the right term).
> Thanks for the help.  The format of the file is below.
> -John
>
> Card Name: Akroma, Angel of Wrath
> Card Color: W
> Mana Cost: 5WWW
> Type & Class: Creature - Angel Legend
> Pow/Tou: 6/6
> Card Text: Flying, first strike, trample, haste, protection from black,
>   protection from red. Attacking doesn't cause Akroma, Angel
>   of Wrath to tap.
> Flavor Text: No rest.  No mercy.  No matter what.
> Artist:  Ron Spears
> Rarity:  R
> Card #:  1/145
>
> Card Name: Akroma's Devoted
> Card Color: W
> Mana Cost: 3W
> Type & Class: Creature - Cleric
> Pow/Tou: 2/4
> Card Text: Attacking doesn't cause Clerics to tap.
> Flavor Text: "Akroma asked for only one thing from her troops: unwavering,
>   unconditional loyalty."
> Artist:  Dave Dorman
> Rarity:  U
> Card #:  2/145
>
> etc.. etc...
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to