Jump to content


Photo

regex help


  • Please log in to reply
3 replies to this topic

#1 esiason14

esiason14
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts

Posted 14 April 2006 - 04:58 PM

I'm trying to get a listing of transactions from this page:

[a href=\"http://www.sportsline.com/mlb/transactions\" target=\"_blank\"]http://www.sportsline.com/mlb/transactions[/a]


I'm using this, which returns the info all jumbled together (see below)

<?php

/*** Load the page here ***/

$input = file_get_contents("http://www.sportsline.com/mlb/transactions");

// *** Finding the team transactions between href tags ***/

$trans= preg_match("@<a[\s]+href=/mlb/teams/transactions/(.*)>(.*)</a>@", $input, $matches);


// Did we find a match?
if ($trans = true) {

// Here it is
echo '<br />' . strip_tags($matches['1']) . '<br>';

} 

?>


Output looks like this:

Washington NationalsRecalled OF Ryan Church and INF Brendan Harris from New Orleans of the PCL. Optioned OF Brandon Watson and C Wiki Gonzalez to New Orleans.Wednesday, April 12, 2006Arizona DiamondbacksPlaced LHP Terry Mulholland on the 15-day DL. Purchased the contract of RHP Casey Daigle from Tucson of the PCL.Atlanta BravesRecalled INF Tony Pena from Richmond of the IL.Baltimore OriolesRecalled RHP Chris Britton from Bowie of the Eastern League. Optioned RHP Cory Morris to Ottawa of the IL.Boston Red SoxAgreed to terms with OF Coco Crisp on a three-year contract extension through 2009. Activated LHP David Wells from the 15-day DL. Placed RHP David Riske on the 15-day DL, retroactive to April 5.Milwaukee BrewersClaimed RHP Vince Perkins off waivers from Toronto.Seattle MarinersActivated OF Matt Lawton. Designated C Guillermo Quiroz for assignment. Optioned RHP Jeff Harris to Tacoma of the PCL. Recalled C Rene Rivera from Tacoma.Tampa Bay Devil RaysPlaced 3B Aubrey Huff on the 15-day DL.Texas RangersPlaced 2B Ian Kinsler on the 15-day DL. Activated OF Gary Matthews Jr. from the 15-day DL. Activated INF Marshall McDougall from the 15-day DL and optioned him to Oklahoma of the PCL.Toronto Blue JaysAssigned C Jason Phillips outright to Syracuse of the IL.

I'm trying to get it where I can show each transaction, by team the specified date. Can someone point me in the right direction and/or offer an example/pointers. THanks

#2 bonaparte

bonaparte
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts
  • Location#172, I Floor, 'Hamsashree', Defence Colony, Sahakaranagar, Bangalore, India

Posted 15 April 2006 - 11:11 AM

[!--quoteo(post=364836:date=Apr 14 2006, 11:58 AM:name=esiason14)--][div class=\'quotetop\']QUOTE(esiason14 @ Apr 14 2006, 11:58 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I'm trying to get a listing of transactions from this page:

[a href=\"http://www.sportsline.com/mlb/transactions\" target=\"_blank\"]http://www.sportsline.com/mlb/transactions[/a]
I'm using this, which returns the info all jumbled together (see below)

<?php

/*** Load the page here ***/

$input = file_get_contents("http://www.sportsline.com/mlb/transactions");

// *** Finding the team transactions between href tags ***/

$trans= preg_match("@<a[\s]+href=/mlb/teams/transactions/(.*)>(.*)</a>@", $input, $matches);
// Did we find a match?
if ($trans = true) {

// Here it is
echo '<br />' . strip_tags($matches['1']) . '<br>';

} 

?>
Output looks like this:

Washington NationalsRecalled OF Ryan Church and INF Brendan Harris from New Orleans of the PCL. Optioned OF Brandon Watson and C Wiki Gonzalez to New Orleans.Wednesday, April 12, 2006Arizona DiamondbacksPlaced LHP Terry Mulholland on the 15-day DL. Purchased the contract of RHP Casey Daigle from Tucson of the PCL.Atlanta BravesRecalled INF Tony Pena from Richmond of the IL.Baltimore OriolesRecalled RHP Chris Britton from Bowie of the Eastern League. Optioned RHP Cory Morris to Ottawa of the IL.Boston Red SoxAgreed to terms with OF Coco Crisp on a three-year contract extension through 2009. Activated LHP David Wells from the 15-day DL. Placed RHP David Riske on the 15-day DL, retroactive to April 5.Milwaukee BrewersClaimed RHP Vince Perkins off waivers from Toronto.Seattle MarinersActivated OF Matt Lawton. Designated C Guillermo Quiroz for assignment. Optioned RHP Jeff Harris to Tacoma of the PCL. Recalled C Rene Rivera from Tacoma.Tampa Bay Devil RaysPlaced 3B Aubrey Huff on the 15-day DL.Texas RangersPlaced 2B Ian Kinsler on the 15-day DL. Activated OF Gary Matthews Jr. from the 15-day DL. Activated INF Marshall McDougall from the 15-day DL and optioned him to Oklahoma of the PCL.Toronto Blue JaysAssigned C Jason Phillips outright to Syracuse of the IL.

I'm trying to get it where I can show each transaction, by team the specified date. Can someone point me in the right direction and/or offer an example/pointers. THanks
[/quote]


What is it that you are trying to match using preg_match()? Try escaping the special characters. Add a \ before the special characters to match the character literally.



#3 bonaparte

bonaparte
  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts
  • Location#172, I Floor, 'Hamsashree', Defence Colony, Sahakaranagar, Bangalore, India

Posted 15 April 2006 - 11:22 AM

[!--quoteo(post=365038:date=Apr 15 2006, 06:11 AM:name=bonaparte)--][div class=\'quotetop\']QUOTE(bonaparte @ Apr 15 2006, 06:11 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
What is it that you are trying to match using preg_match()? Try escaping the special characters. Add a \ before the special characters to match the character literally.
[/quote]


Does the below code make sense?

<?php

/*** Load the page here ***/

$input = file_get_contents("http://www.sportsline.com/mlb/transactions");

// *** Finding the team transactions between href tags ***/

$trans= preg_match("/div.class=SLTables1>.+/", $input, $matches);


// Did we find a match?
if ($trans = true) {

// Here it is
//echo '<br />' . strip_tags($matches['1']) . '<br>';
print_r($matches);

}

?>


#4 ypirc

ypirc
  • Members
  • PipPipPip
  • Advanced Member
  • 54 posts

Posted 16 April 2006 - 04:28 AM

Okay, first of all in your code you are doing if($trans = true), which is going to SET $trans to true so it's ALWAYS going to be true. When evaluating a variable on it's contents, please remember to use "==" not "=". Anyway, onto the good stuff. I've re-written your code pretty much completely and have come up with my own solution to your problem, giving you the date, the team, and the article all seperate. Please see below:

$input = file_get_contents("http://www.sportsline.com/mlb/transactions");
preg_match_all("#(?<=<font class=bg0font>)(?P<D>.+)(?=</font>)(?P<C>.+)(?=</tr><tr align=left class=bg0>)#U", $input, $match);

foreach($match['D'] as $key => $date) {
        printf("DATE: %s\n", $date);
        $trans = preg_match_all("#(?:<a href=/mlb/teams/transactions/[A-Z]{2,3}>)(?P<T>.+)(?:</a></td><td  align=left>)(?P<I>.+)(?:</td>)#U", $match['C'][$key], $matches);

        foreach($matches['T'] as $key => $team) {
                printf("Team: %s Story: %s \n", $team, $matches['I'][$key]);
        }
}

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
%php -f test.php
DATE: Saturday, April 15, 2006
Team: Boston Red Sox Story: Placed LHP David Wells on the 15-day DL. Called up RHP Jermaine Van Buren from Pawtucket of the IL.
Team: Chicago Cubs Story: Optioned RHP Michael Wuertz to Iowa of the PCL. Recalled RHP Roberto Novoa from Iowa.
Team: Cleveland Indians Story: Placed RHP Fernando Cabrera on the 15-day DL. Recalled RHP Fausto Carmona from Buffalo of the IL.
Team: Detroit Tigers Story: Placed INF Dmitri Young on the 15-day DL. Called up OF Alexis Gomez from Toledo of the IL.
Team: New York Yankees Story: Signed INF Carlos Pena and RHP Jesus Colome to minor league contracts.
Team: Pittsburgh Pirates Story: Placed 1B Sean Casey on the 15-day DL. Purchased the contract of INF-OF Mike Edwards from Indianapolis of the IL. Transferred RHP Kip Wells from the 15- to the 60-day DL.
Team: Toronto Blue Jays Story: Activated RHP A.J. Burnett from the 15-day DL.
Team: Washington Nationals Story: Placed RHP Ryan Drese on the 15-day DL. Purchased the contract of RHP Saul Rivera from New Orleans of the PCL.
[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users