Huge string to parse somehow??

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
FredEH
Forum Newbie
Posts: 18
Joined: Fri May 09, 2003 8:39 am

Huge string to parse somehow??

Post by FredEH »

Hi, I really need some help. I have checked out the manual on php.net but its really not helping me. If anyone can lend a hand I'd appreciate it.

I am pulling some info from another website (with permission to do so) and loading it into a string that ends up looking like this:

Code: Select all

03/08/03 1300     Visitor...... 21  Home...... 14   (at Blah)
      03/15/03 0900     Visitor...... 15  Home......  1   (at Blah)
      03/22/03 1300     Visitor...... 14  Home...... 11   (at Blah)
      04/05/03 1400     Visitor...... 20  Home......  4   (at Blah)
      04/12/03 0900     Visitor...... 25  Home......  0   (at Blah)
      04/19/03 1300     Visitor......  9  Home......  0   (at Blah)
      04/26/03 1300     Visitor...... 12  Home......  7   (at Blah)
      05/03/03 0900     Visitor...... vs  Home......      (at Blah)
      05/10/03 0900     Visitor...... 17  Home...... 10   (at Blah)
      05/17/03 1300     Visitor...... vs  Home......      (at Blah)
      05/31/03 1300     Visitor...... vs  Home......      (at Blah)
      06/07/03 1300     Visitor...... vs  Home......      (at Blah)
      06/14/03 0900     Visitor...... vs  Home......      (at Blah)
      06/14/03 1300     Visitor...... vs  Home......      (at Blah)
      06/21/03 0900     Visitor...... vs  Home......      (at Blah)
      06/28/03 0900     Visitor...... vs  Home......      (at Blah)
      07/12/03 1300     Visitor...... vs  Home......      (at Blah)
Where "visitor" and "home" are different team names of different lengths. And blah is the location of the game. The string also includes all of the whitespace shown. The way I am doing it now, I just echo the string inside <pre> tags which is not very stylish. I would like to assign variables for the date, time, team, and location. Does anyone have any idea how I can accomplish this?
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Looks like some RegEx is in order
FredEH
Forum Newbie
Posts: 18
Joined: Fri May 09, 2003 8:39 am

Post by FredEH »

Thanks Gleeb,

So it looks like maybe I need to use preg_match but I'm having troulbe understanding how its used... :?:
FredEH
Forum Newbie
Posts: 18
Joined: Fri May 09, 2003 8:39 am

Post by FredEH »

Ok... I tried this to parse the string for the date:

Code: Select all

<?php
  $s = "|[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}|";
  preg_match($s, $html, $matches);
  $date = $matches[1];
?>
It doesn't work. I echo $date and get nothing. What am I doing wrong?

Thanks.

EDIT:
I changed it to this:

Code: Select all

<?php
  $s = "|[0-9]{1,2}/[0-9]{1,2}/[0-9]{1,2}|";
  preg_match($s, $html, $matches);
  $date = $matches[0];
?>
And it works, but only gets 1 date. I'll check on that.
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

I think it's matching on nothing. I could be wrong. Try...

Code: Select all

<?php
$s="^([0-9]{2}/[0-9]{2}/[0-9]{2})^"
?>
or maybe if you want each part seperatly...

Code: Select all

<?php
$s="^([0-9]{2})/([0-9]{2})/([0-9]{2})^"
?>
EDIT ABOUT YOUR EDIT :P:
try preg_match_all()
FredEH
Forum Newbie
Posts: 18
Joined: Fri May 09, 2003 8:39 am

Post by FredEH »

Yeah.. preg_match_all works for the dates.... now I have all the dates in an array which is great.

Any Idea how I can get the teams? I don't know these regular expressions good enough.. :(
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Code: Select all

03/08/03 1300     Visitor...... 21  Home...... 14   (at Blah)

Code: Select all

<?php
$s ="^"; // Starts the regex
$s ="([0-9]{2})/([0-9]{2})/([0-9]{2})"; // Dates
$s.="\s+([0-9]{4})"; // Time in European format
$s.="\s+([ \w]+).+"; // First team
$s.="\s+([0-9]+)"; // First Team Score
$s.="\s+([ \w]+).+"; // 2nd team
$s.="\s+([0-9]+)"; // 2nd Team Score
$s.="\s+\\(at ([/w .,]+)\\)\s+"; // Venue
?>
I don't guarantee that it'll work, but it's a step in the right direction.
FredEH
Forum Newbie
Posts: 18
Joined: Fri May 09, 2003 8:39 am

Post by FredEH »

Thanks gleeb,

When I do this:

Code: Select all

<?php
$s ="^"; // Starts the regex 
$s ="([0-9]{2})/([0-9]{2})/([0-9]{2})"; // Dates 
$s.="\s+([0-9]{4})"; // Time in European format 
$s.="\s+([ \w]+).+"; // First team 
$s.="\s+([0-9]+)"; // First Team Score 
$s.="\s+([ \w]+).+"; // 2nd team 
$s.="\s+([0-9]+)"; // 2nd Team Score 
$s.="\s+\\(at ([/w .,]+)\\)\s+"; // Venue 

preg_match($s, $html, $match);
$output = $match[0];
print $output;
?>
I get this warning

Code: Select all

Unknown modifier '/'
On the line where the preg_match is. Any ideas?
Gleeb
Forum Commoner
Posts: 87
Joined: Tue May 13, 2003 7:01 am
Location: UK
Contact:

Post by Gleeb »

Code: Select all

<?php 
$s ="^"; // Starts the regex 
$s.="([0-9]{2})/([0-9]{2})/([0-9]{2})"; // Dates // Changed this to .= instead of =
$s.="\s+([0-9]{4})"; // Time in European format 
$s.="\s+([ \w]+).+"; // First team 
$s.="\s+([0-9]+)"; // First Team Score 
$s.="\s+([ \w]+).+"; // 2nd team 
$s.="\s+([0-9]+)"; // 2nd Team Score 
$s.="\s+\\(at ([/w .,]+)\\)\s+"; // Venue 
$s.="^"; // Ends the regex // Added this
?>
If that doesn't fix it, change the appropriate line with...

Code: Select all

$s.="([0-9]{2})[/]([0-9]{2})[/]([0-9]{2})"; // Dates // Changed this to .= instead of =
Post Reply