Search code examples
phphtmlsimple-html-dom

PHP Simple HTML DOM Getting ONLY first 5 links inside a div class


So far I have this

<?PHP include('simple_html_dom.php');
$html = file_get_html('http://www.mangastream.com/');
foreach($html->find('.side-nav') as $t)
foreach($t->find('a')as $k)
echo $k->href . '<br>'; 
?>

which outputs all the links from inside the class. but I just want to have the first 5 links.


Solution

  • try that

    <?PHP include('simple_html_dom.php');
    $html = file_get_html('http://www.mangastream.com/');
    foreach($html->find('.side-nav') as $t){
        foreach($t->find('a')as $key => $k){
            echo $k->href . '<br>';
            if($key >= 4){
                break;
            }
        }
    }
    ?>