tags:

views:

48

answers:

2

Alright, so here's my problem. I'm trying to write a script in PHP that will parse our workorder system and return a set of tickets but I've run into a bit of a snag trying to parse the ticket list. I've been trying to use regex as much as possible to force myself to learn the syntax and I could sware that this should work but alas, it's not and so I come here seeking Your collective wisdom.

<tr>
   ...
   ...
   ...
   ...
</tr>

I am trying to retrieve the block between the tags here so that I can parse that down again for specific information. The block size is pretty regular but lines between the tags might vary based on the length of the description in the ticket. The regex that I'm currently employing is

/<tr>(.+)<\/tr>/

This seems the smallest way to achieve my goal but I am getting errors from preg_match. I realize I could flag and loop it as in this very very rough pseudo code

if /<tr>/ then {
   while != /<\/tr>/ {
      store line
   }
}

however my goal here is to gain a better understanding of regex and how to use it.

+2  A: 
  • Maybe you need the s (PCRE_DOTALL) modifier, to match over multiple lines.
  • Maybe you want .*? instead of .*, or the U (PCRE_UNGREEDY) modifier to match non-greedy.
Sjoerd
+1  A: 

Use Simple HTML DOM.

Regex parsing html is a mess.

simplemotives
In short yes. Sorry it took so long to select an answer for this but I figured out what the problem was. I was trying to parse blocks of text that were far far to large for a regular expression. I fixed it by throwing in some for next loops to keep track of how deep into certain tags I was. Great news is that the app works and now I get a text message on weekdays if there are calendar entries on the website, even parses multiple entries on a single day, pretty proud of myself and was easier than I originally thought.
Melignus