regular expressions help please!!!

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
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

regular expressions help please!!!

Post by romeo »

I have a huge text file that I am trying to arrange the out put in but can't seem to match...

the file looks like this

Code: Select all

MRD-031 Leghul
You can choose which you attack, monster or a
player. If you attack a player, your opponent can’t
defend - attack goes directly against opponent’s Life
Points.
MRD-032 Ooguchi
You can choose which you attack, monster or a
player. If you attack a player, your opponent can’t
defend - attack goes directly against opponent’s Life
Points.
MRD-033 Leogun
MRD-034 Blast Juggler
Either player’s monsters can be destroyed. They must
be face-up. You can NOT destroy this monster itself
by this card’s effect. If there are less than 3 monster
including this monster, you can not activate this card’s
effect. A player of this card can choose when to
activate its effect.
MRD-035 Jinzo #7
You can choose which you attack, monster o
You have the ([A-Z]{3})-(\d\d\d) as the card number
everything after that \w till end of line is the card name
everything under that until another card name is the cards description...


I tried matching ([A-Z]{3})-(\d\d\d) *? (\w*)[\n|\r]
to grabe the first line and replace is with ><$1-$2|$3|

so i can encase and then remove \n to put in a CVS but it doesnt match anything.
Please help.. and if possable explain it to me so I can learn these damn things better
drew78
Forum Newbie
Posts: 2
Joined: Thu Jul 24, 2003 7:08 am

Post by drew78 »

This might help:
http://www.hgmp.mrc.ac.uk/Software/EMBO ... /preg.html

This script works, but is sorta a ugly-hack...

Code: Select all

<?php
$text = "MRD-031 Leghul 
You can choose which you attack, monster or a 
player. If you attack a player, your opponent can’t 
defend - attack goes directly against opponent’s Life 
Points. 
MRD-032 Ooguchi 
You can choose which you attack, monster or a 
player. If you attack a player, your opponent can’t 
defend - attack goes directly against opponent’s Life 
Points. 
MRD-033 Leogun s
MRD-034 Blast Juggler 
Either player’s monsters can be destroyed. They must 
be face-up. You can NOT destroy this monster itself 
by this card’s effect. If there are less than 3 monster 
including this monster, you can not activate this card’s 
effect. A player of this card can choose when to 
activate its effect.";
  
function printCard($cardID, $name, $info)
{
  echo(addslashes("$cardID $name\n$info\n---------\n"));
}  
  $text = str_replace("\r", "\n", $text);
  $text = explode("\n", $text);

  $p_cardID = "[A-Z]{3}-[0-9]{3})\s([\w| ]+";
  $p_break = "(\\n|\\r)";
  while(list($k, $line) = each($text))
  {
    if(preg_match("(($p_cardID))", $line, $match))
    {
      if(isset($cardID)) // don't print on first card
        printCard($cardID, $name, $info);
      $cardID = $match[1];
      $name   = $match[2];
      $info   = "";
    }
    else
      $info .= $line;
  }
  printCard($cardID, $name, $info);
?>
romeo
Forum Contributor
Posts: 138
Joined: Sun Apr 21, 2002 12:50 pm

Post by romeo »

that looks like it breaks on each line and there are several lines just for one part of the description.

is there a way to do that reg match like i was doing it or not worth the hassle? :)
Post Reply