Search code examples
phpsimple-html-dom

better way with Simple HTML Dom Parser


my HTML code is THE CODE IS REPEATED 16 times :

<div class="headline_image">
<a ga-cat="slideshow-view" ga-action="view-1" href="mylink"><img src="http://dd4994.jpg" width="420" height="323" align="right" alt="my text "/></a>
</div>

I WANT TO GET all the imgs links and text also href what i did :

for ($x = 0; $x <=  15; $x++) {
$imglink = $html->find('div[class=headline_image] img', $x)->getAttribute('src');
$mytext = $html->find('div[class=headline_image] img', $x)->getAttribute('alt');
$postlink = $html->find('div[class=headline_image] a', $x)->getAttribute('href');
  echo '<br/>';
    echo $mytext;
     echo '<br/>';
    print_r($postlink);
    echo '<br/>';
    }

the code is slow any changes ?


Solution

  • You slow down your code by using too much anonymous objects. It means you don't put the result of the function into a variable, rather just use it "on the go". This needs to run your function again and again slowing down your project.

    Because you can use the function find to return an array, I advice you to do so before the for loop.

    $imgarray = $html->find('div[class=headline_image] img', $x);
    

    This way you run $html->find exactly once, and not sixteen times. In the for loop you can use it as an array and work with the results: $imgarray[$x]. You make the same for $anchorarray and your code will speed up, you'll see.

    Alternative solution is using PHP DOM $childNodes on the container in which this 16 item can be found (or the body element). This will return the sixteen div elements in which you can navigate by calling $firstChild for the <a> element and $firstChild again for the <img> element. Probably this is more secure in case you want to make changes to the website (like adding more content to the end etc.)