1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Matching a link in regex

Discussion in 'PHP' started by Python, Jan 6, 2009.

  1. #1
    I have a chunk of HTML code which I need to 'analyze'. It essentially contains a table of links.

    All I need to do is to extract the anchor text from each of the links and put them into an array.

    So consider the following (a few rows) I would need to be able to extract the words "link one", "link two" and "link three". There will of course be more rows than this in the final code.

    <td><a href="/link-to-a-page">link one</a></td>
    <td><a href="/link-to-notherpage">link two</a></td>
    <td><a href="/whatever-the-heck">link three</a></td>
    HTML:
    Any help would be much appreciated.

    Thanks
    SEMrush
     
    Python, Jan 6, 2009 IP
    SEMrush
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    <?php
    
    $list = <<<list
    <td><a href="/link-to-a-page">link onea</a></td>
    <td><a href="/link-to-notherpage">link two</a></td>
    <td><a href="/whatever-the-heck">link three</a></td>
    list;
    
    preg_match_all('/<td><a href="(?<url>[^"]+)">(?<text>[^<]+)<\/a><\/td>/i', $list, $matches, PREG_SET_ORDER);
    
    var_dump($matches);
     
    Danltn, Jan 6, 2009 IP
  3. Python

    Python Well-Known Member

    Messages:
    680
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    120
    #3
    Thanks for the help, perfect!
     
    Python, Jan 6, 2009 IP