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 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?
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: