Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SimpleHTMLDom: Call to a member function find() on array

So i want to loop trough specific TD's in a LARGE html page. I'm using simplehtmldom in order to achieve that. The problem is that i cant make it work without putting every step in a foreach.

Here is my php

include('../inc/simple_html_dom.php');
$html = file_get_html("http://www.bjork-family.com/f43-london-stories");
// I just put the dom of pagebody into TEST
$test =  $html->find('#page-body');
foreach($test->find('img') as $element) 
{
    echo "<img src='" . $element->src . "'/>" . '<br>';
}

i get this error

Fatal error: Call to a member function find() on array in /mywebsite.php on line 39

line 39 is this one

foreach($test->find('img') as $element) 

I tried a lot of different things, if i keep it really really simple like that :

// Create DOM from URL or file
$html = file_get_html('http://www.bjork-family.com/f43-london-stories');
foreach($html->find('img') as $element) 
       echo $element->src . '<br>'; 

then it works !

So it seems to be working when i go like that

foreach($html->find('div') as $element) 
    {
        if($element->id == 'page-body')
        {
            echo $element->id;
            echo "EXIST <br>";
        }
        // echo "<img src='" . $element->src . "'/>" . '<br>';
    }

But i don't want to search into my html only using foreach, is there another way where i can get to my position and then do i loop (i have to loop trough tr in a table )

like image 779
Bidoubiwa Avatar asked Jun 07 '26 15:06

Bidoubiwa


1 Answers

$test =  $html->find('#page-body');

Now $test is an array

$test =  $html->find('#page-body', 0);

Now $test is an element

foreach($test->find('img') as $element) 
{
    echo "<img src='" . $element->src . "'/>" . '<br>';
}

This will work now. Also you can simplify with:

foreach($test->find('#page-body img') as $element) 
{
    echo "<img src='" . $element->src . "'/>" . '<br>';
}
like image 164
pguardiario Avatar answered Jun 09 '26 03:06

pguardiario



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!