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.

Need help with extracting links using PHP regex (preg_match_all)

Discussion in 'PHP' started by komirad, Jul 15, 2009.

  1. #1
    The links in the html looks something like this, with many iterations:

    
    <h2 class="lists">Section Name(variable1)</h2>
    
    <ul class="listings">
    
    <li><a href="variable2" title="variable3">variable4</a></li>
    <li><a href="variable2" title="variable3">variable4</a></li>
    <li><a href="variable2" title="variable3">variable4</a></li>
    <li><a href="variable2" title="variable3">variable4</a></li>
    
    </ul>
    
    Code (markup):
    I need the links to be seperated by the section name.
    So the result array would be something like this, with each item contain the details of the link such as the title, url and anchor text:

    Section 1:
    link1, link2, link3
    Section 2:
    link1, link2, link3


    SEMrush
    I managed to extract the links, but I couldn't find to list them according to their Sections with the following code:
    preg_match_all('/href="([^"]+)"[^>]*>([^<]+)/',$pageCode,$resultArray);
    Code (markup):
    Any help? :D
     
    komirad, Jul 15, 2009 IP
    SEMrush
  2. websecrets

    websecrets Peon

    Messages:
    97
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What output did you get when you extracted the links?
     
    websecrets, Jul 15, 2009 IP
  3. matthewrobertbell

    matthewrobertbell Peon

    Messages:
    781
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try using explode on the section name, then run your regex on each section individually
     
    matthewrobertbell, Jul 17, 2009 IP
  4. Goramba

    Goramba Peon

    Messages:
    128
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Kinda tricky. This could probably be cleaned up.

    CODE:

    <?php
    $string= '<h2 class="lists">Section Name(variable1)</h2>
    
    <ul class="listings">
    
    <li><a href="variable1" title="variable1">variable1</a></li>
    <li><a href="variable2" title="variable2">variable2</a></li>
    <li><a href="variable3" title="variable3">variable3</a></li>
    <li><a href="variable4" title="variable4">variable4</a></li>
    
    </ul>
    
    
    <h2 class="lists">Section Name(variable2)</h2>
    
    <ul class="listings">
    
    <li><a href="variable5" title="variable5">variable5</a></li>
    <li><a href="variable6" title="variable6">variable6</a></li>
    <li><a href="variable7" title="variable7">variable7</a></li>
    <li><a href="variable8" title="variable8">variable8</a></li>
    
    </ul>';
    
    preg_match_all("/(<h2 class=\"lists\">.*?)<\/ul>/s", $string, $matches);
    foreach ($matches[1] as $match)
    {
    preg_match_all("/<li><a href=\".*?\" title=\".*?\">(.*)<\/a><\/li>/i", $match, $matches2);
    preg_match("/<h2 class=\"lists\">(.*?)<\/h2>/i", $match, $matches3);
    echo $matches3[1]."<br>";
    $comma = implode(",", $matches2[1]);
    echo $comma;
    echo "<br><br>";
    }
    PHP:
    RESULT:

    Section Name(variable1)
    variable1,variable2,variable3,variable4
    
    Section Name(variable2)
    variable5,variable6,variable7,variable8
    PHP:
     
    Goramba, Jul 17, 2009 IP