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
https://forums.phpfreaks.com/topic/27300-regexp-html/
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
https://forums.phpfreaks.com/topic/27300-regexp-html/#findComment-124935
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
https://forums.phpfreaks.com/topic/27300-regexp-html/#findComment-125377
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.