quasiman Posted September 14, 2007 Share Posted September 14, 2007 I'm trying to use the text lists from the card game "Magic: The Gathering", and insert the data into a mysql table. I found a basic script that is supposed to do it, I've modified it a little for some basic errors I found, but it's still not quite working. Here's the first 2 records in the file so you can see how it looks to me: Card Title: Air Elemental Color: Blue Card Type: Summon Elemental Cost: 3UU Pow/Tgh: 4/4 Card Text: Flying Artist: Richard Thomas Rarity: Uncommon 1 Card Title: Ancestral Recall Color: Blue Card Type: Instant Cost: U Pow/Tgh: n/a Card Text: Draw 3 cards or force opponent to draw 3 cards. Artist: Mark Poole Rarity: Rare 1 Here's the results/insert that I get when running the file: Card Type: Summon Elemental Cost: 3UU Pow/Tgh: 4/4 Card Text: Flying Artist: Richard Thomas Rarity: Uncommon 1 Card Title: Ancestral Recall Cost: U Pow/Tgh: n/a Card Text: Draw 3 cards or force opponent to draw 3 cards. Artist: Mark Poole Rarity: Rare 1 Now here's the file I'm using: <?php $db = "mtg_db"; $table= "mtg_table"; $user = "username"; $pass = "password"; // get info from text file and put it in a mysql table // run it once only, otherwise you'll get duplicate records $cards= 10; /*put the number of cards in the text file here*/ $fp=fopen("alpha-beta-unlm.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. /*preg_replace gets rid of the extra text, and trim gets rid of any spaces before and after the part you want. */ $temp=fgets($fp,100); $card_name=trim(preg_replace("/Card Title:/","",$temp)); echo $card_name."<br>\n"; $temp=fgets($fp,100); $card_color=trim(preg_replace("/Color:/","",$temp)); echo $card_color."<br>\n"; $temp=fgets($fp,100); $card_type=trim(preg_replace("/Cart Type:/","",$temp)); echo $card_type."<br>\n"; $temp=fgets($fp,100); $mana_cost=trim(preg_replace("/Cost:/","",$temp)); echo $mana_cost."<br>\n"; $temp=fgets($fp,100); $pow_tou=trim(preg_replace("/Pow_Tgh:/","",$temp)); echo $pow_tou."<br>\n"; $temp=fgets($fp,100); $card_text=trim(preg_replace("/Card Text:/","",$temp)); echo $card_text."<br>\n"; $temp=fgets($fp,100); $artist=trim(preg_replace("/Artist:/","",$temp)); echo $artist."<br>\n"; $temp=fgets($fp,100); $set_rarity=trim(preg_replace("/Rarity:/","",$temp)); echo $set_rarity."<br>\n"; /*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_title, color, card_type, cost, pow_tou, card_text, artist, rarity) values ('".addslashes($card_name)."', '".addslashes($card_color)."', '".addslashes($mana_cost)."', '".addslashes($card_type)."', '".addslashes($pow_tou)."', '".addslashes($card_text)."', '".addslashes($artist)."', '".addslashes($set_rarity)."') "; $result=mysql_query($query); if (!$result) die ("failed on insert ".mysql_error()); } mysql_close($link); fclose($fp); ?> Any idea why I'm losing the Card Title and Cost on the first record? If I include more data, it does the same thing every other record. And while I'm thinking about it, how can I remove the "Cart Type:", "Cost:", etc,etc from the results and insert? I've looked up preg_replace, but I don't quite understand how to make it work here. Quote Link to comment https://forums.phpfreaks.com/topic/69305-solved-preg_replacetrim-text-file-into-mysql-table/ Share on other sites More sharing options...
quasiman Posted September 17, 2007 Author Share Posted September 17, 2007 Nevermind, the data isn't consistent enough to make it save time or effort doing it this way. Quote Link to comment https://forums.phpfreaks.com/topic/69305-solved-preg_replacetrim-text-file-into-mysql-table/#findComment-349821 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.