preg_match_all with underscores?

Discussion in 'PHP' started by mokimofiki, Sep 25, 2012.

  1. #1
    I am pulling names from rows using preg_match_all and for whatever reason underscores kill it. The code I have that works is:
    
    preg_match_all("'<tr id=\"row(.*?)<\/tr>'si", $html, $matches);
    foreach($matches[0] as $match){
    		$playerRow[] = $match;
    	}
    
    Code (markup):
    The issue is when I change the part that says \"row to \"row_0_0 it breaks as follows.
    
    preg_match_all("'<tr id=\"row_0_0_(.*?)<\/tr>'si", $html, $matches);
    foreach($matches[0] as $match){
    		$playerRow[] = $match;
    	}
    
    Code (markup):
    Any thoughts would be appreciated. obviously the usage of underscores in this way causes the issue although i'm not sure how to escape them or whatever to read them correctly.

    Thank you
     
    mokimofiki, Sep 25, 2012 IP
  2. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #2
    plussy, Sep 26, 2012 IP
  3. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #3
    Thank you for your response and the nice cheat sheet. I didn't think you had to escape underscores but for whatever reason it just will not work with them. Below is a piece of the html that I am scraping from:

    
    <tr id="row_0_0_0" class="row">
    <td class="cell cellLeft"><div class="pName injurable"><a href="/nfl/player?playerId=326&amp;leagueId=94458" id="ttId0_0">Philip Rivers</a><img src="http://d1h60c43tcq0zx.cloudfront.net/static/images/pixel.gif" alt="" class="keeper tooltip" id="ttId1_0" /><img src="http://d1h60c43tcq0zx.cloudfront.net/static/images/pixel.gif" alt="" class="tooltip latestNews tooltip" id="ttId2_0" /></div></td>
    <td class="cell"><span class="position">QB</span></td><td class="cell"><span class="proTeam">SD</span></td><td class="cell cellRight">7</td>
    </tr>
    <tr id="row_0_0_1" class="rowOdd">
    <td class="cell cellLeft"><div class="pName injurable"><a href="/nfl/player?playerId=6641&amp;leagueId=94458" id="ttId7_0">Ben Tate</a><img src="http://d1h60c43tcq0zx.cloudfront.net/static/images/pixel.gif" alt="" class="tooltip latestNews tooltip" id="ttId8_0" /></div></td>
    <td class="cell"><span class="position">RB</span></td><td class="cell cellLeft"><span class="fp"><span class="tooltip error" id="ttId6_1">0.6↓</span></span></td><td class="cell cellRight"><span class="fp"><span>8.37</span></span></td>
    </tr>
    
    Code (markup):
    There are other rows that are grabbed after these if i'm not specific down to the row_0_0. Thank you.
     
    mokimofiki, Sep 26, 2012 IP
  4. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #4
    ok and what do you want to get out of there? the content of tr or what?
     
    plussy, Sep 26, 2012 IP
  5. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #5
    Yes the content of tr so that I can later pull the entire row as needed
     
    mokimofiki, Sep 26, 2012 IP
  6. plussy

    plussy Peon

    Messages:
    152
    Likes Received:
    5
    Best Answers:
    9
    Trophy Points:
    0
    #6
    try this

    
    preg_match_all('|<tr id="row[0-9_]*?" class="row.*?">(.*?)</tr>|si',$html,$matches);
    
    PHP:
     
    plussy, Sep 26, 2012 IP
    mokimofiki likes this.
  7. mokimofiki

    mokimofiki Well-Known Member

    Messages:
    444
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    130
    #7
    Thank you this seems to be the right direction. My next issue is that this brings in all rows ie.
    row_0_0_0
    row_0_0_1
    row_0_1_0

    I need it to only grab rows that start with row_0_0 and not row_0_1.

    I have tried modifying what you have given me although i really am not understanding the pre_match_all usage very well and need to read up on it a bit more before starting anything like this again :)

    Thank you for all of your help.

    EDIT: Nevermind everything is fine thank you very much it works. I messed something else up when I was testing.
     
    Last edited: Sep 26, 2012
    mokimofiki, Sep 26, 2012 IP