SimpleHTMLDom: Call to a member function find() on

2019-05-10 18:54发布

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 )

1条回答
【Aperson】
2楼-- · 2019-05-10 19:29
$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>';
}
查看更多
登录 后发表回答