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
     
    Python, Jan 6, 2009 IP
  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);
    PHP:
     
    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