Okay, I've searched around on this forum for the past half an hour or so, and although I've found things similar to what I'm looking for, it's not quite what I need.
Here's my problem:
I have a text file that has numerous entries like so (these are M:TG cards incase anyone doesn't recognize it 🙂):
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
Card Title: Animate Artifact
Color: Blue
Card Type: Enchant Artifact
Cost: 3U
Pow/Tgh: n/a
Card Text: Target artifact is now a creature with both power and
toughness equal to its casting cost; target retains all its
original abilities as well. This will destroy artifacts with 0
casting cost.
Artist: Douglas Schuler
Rarity: Uncommon 1
And here's my PHP script that I'm using to input the card entries into a database:
<?php
$dbhost = "localhost";
$dbuser = "user";
$dbpass = "pass";
$dbase = "magic";
$cx = mysql_connect($dbhost,$dbuser,$dbpass) or die ("Could not connect to the database server.");
mysql_select_db($dbase) or die ("Could not access the database.");
$set = "Alpha";
// Initialize our arrays
$title_array = array();
$color_array = array();
$ctype_array = array();
$cost_array = array();
$pt_array = array();
$text_array = array();
$artist_array = array();
$rarity_array = array();
$file = "list.txt";
$handle = fopen($file,"r+b");
$string = fread($handle,filesize($file));
preg_match_all("#^Card Title:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
$title_array[]= $matches[1][$i];
}
preg_match_all("#^Color:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
$color_array[]= $matches[1][$i];
}
preg_match_all("#^Card Type:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
$ctype_array[]= $matches[1][$i];
}
preg_match_all("#^Cost:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
$cost_array[]= $matches[1][$i];
}
preg_match_all("#^Pow/Tgh:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
$pt_array[]= $matches[1][$i];
}
preg_match_all("#^Card Text:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
echo $matches[1][$i]."<br>";
$text_array[]= $matches[1][$i];
}
preg_match_all("#^Artist:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
$artist_array[]= $matches[1][$i];
}
preg_match_all("#^Rarity:\\s{0,}(.*)#m",$string,$matches);
for($i = 0; $i < count($matches[1]); $i++)
{
$rarity_array[]= $matches[1][$i];
}
for($i = 0; $i < count($title_array); $i++)
{
$sql = "INSERT INTO card_list VALUES ('','".$title_array[$i]."','".$color_array[$i]."',
'".$ctype_array[$i]."','".$cost_array[$i]."','".$pt_array[$i]."','".$text_array[$i]."',
'".$artist_array[$i]."','".$rarity_array[$i]."','".$set."')";
mysql_query($sql) or die ("Could not execute query.".mysql_error());
}
fclose($handle);
mysql_close($cx);
?>
It all works fine, except for the Card Text entries. Since some of the cards have text that spills over to new lines, the preg_match_all is only grabbing the first line and inserting that into the database.
I either need to stick all text after Card Text: but before Artist: onto one line, or somehow write an expression that will grab the text between Card Text: and Artist: regardless of whether it's on 1, 2 or 50 lines.
You'll notice that if the text is more than one line for Card Text: that there are two tabs after the newline as well, which causes problems for the first solution I've tried (that is, taking all the text after Card Text: and splicing it into one lone line).
Any help is appreciated.