Search code examples
phparraysforeachhtml-parsingsimple-html-dom

Generate an array of href strings from all <a> tags in an HTML document using Simple HTML DOM Parser


I want to fill an array with the links that I get from this foreach. How can I do that?

foreach($html->find('a') as $link) {
       echo $link->href; //output: link1.html link2.html link3.html......
}

Solution

  • It's simple, Try this :-

    $dataArray = array();
    foreach($html->find('a') as $link) {
           $dataArray[] = $link->href; 
    }
    
    echo '<pre>';
    print_r($dataArray);
     echo '</pre>';