Jump to content

Regexp html


esiason14

Recommended Posts

I've been trying to match this for quite a while...and with no luck. Can someone please help me out here.
HTML to match (there's actually going to be about 10 players to match):

[code]<tr  height=17  class=bg2 align=right valign=middle><td  align=middle>21</td><td  align=left><a href="/nba/players/playerpage/6552">Tim Duncan</a></td><td >6</td><td >6</td><td >21.5</td><td >49</td><td >91</td><td >53.8</td><td >31</td><td >57</td><td >54.4</td><td >0</td><td >1</td><td >0</td></tr><tr  height=17  class=bg2 align=right valign=middle><td  align=middle>20</td><td  align=left><a href="/nba/players/playerpage/139066">Manu Ginobili</a></td><td >6</td><td >6</td><td >13.0</td><td >25</td><td >64</td><td >39.1</td><td >20</td><td >25</td><td >80.0</td><td >8</td><td >24</td><td >33.3</td></tr>[/code]


What I want to match is:

/nba/players/playerpage/6552 (the href), Player Name (within the a tags), and then everything between the <td> </td> tags.

So a complete match would look like this:
[code]/nba/players/playerpage/6552, Tim Duncan, 6, 6, 21.5, 41, 91, 53.8, 31, 57, 54.4, 0, 1, 0[/code]

Thanks to everyone in advance
Link to comment
Share on other sites

How about something like this?

[hr]
[code=php:0]preg_match_all('/(?<=<a href=").*?(?=">)|[^<>]+(?=<\/a>)|(?<=<td >).*?(?=<)/', $string, $matches);

$matches = array_chunk($matches[0], 14);
print_r($matches);[/code]
[hr]


Gives me an array() like this

[code]Array
(
    [0] => Array
        (
            [0] => /nba/players/playerpage/6552
            [1] => Tim Duncan
            [2] => 6
            [3] => 6
            [4] => 21.5
            [5] => 49
            [6] => 91
            [7] => 53.8
            [8] => 31
            [9] => 57
            [10] => 54.4
            [11] => 0
            [12] => 1
            [13] => 0
        )

    [1] => Array
        (
            [0] => /nba/players/playerpage/139066
            [1] => Manu Ginobili
            [2] => 6
            [3] => 6
            [4] => 13.0
            [5] => 25
            [6] => 64
            [7] => 39.1
            [8] => 20
            [9] => 25
            [10] => 80.0
            [11] => 8
            [12] => 24
            [13] => 33.3
        )

)[/code]
Link to comment
Share on other sites

Thanks, Nicklas. That's really, really close to what I'm looking for. The only problem I run into is if the string I'm trying to match has other <a href ....then it matches all of the links. I only want to match the ones with /nba/players/playerpage/...... Is there a way to narrow the regex match to only these?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.