Skip to content
Advertisement

Simple HTML DOM Parser – Problem displaying a variable inside foreach loop

I have created a simple PHP script using the simple_html_dom.php class. I fetch some information about movies from a website. I have one foreach loop inside another foreach loop. When I try to display the moviename inside the foreach loop I get the last moviename. What I want to achieve is to get each one of the unique movienames in each item. The problem is with the $movie variable.

(When i echo the $movie var on line 27 i get the correct result but I want to have each moviename inside the youtube links on line 33…)

<?php
include("simple_html_dom.php");
    
$tpb = 'https://tpb.party/search/2020/1/99/200';
$html = file_get_html(html_entity_decode($tpb));
    
foreach($html->find('tr.header') as $header) {
    $header->outertext = '';
}
        
foreach($html->find('td') as $bottom) {
    if ($bottom->colspan == '9') {
        $bottom->outertext = '';
    }
}
        
foreach($html->find('td.vertTh') as $vert) {
    $vert->outertext = '';
}   
    
foreach($html->find("div.detName") as $movie) {
    $movie = $movie->plaintext;
    echo $movie;    //Works Okey, it displays each of the movietitles
    
    foreach($html->find('img') as $img) {
    
        if ($img->outertext == '<img src="https://tpb.party/static/img/11x11p.png" height="11" width="11">') {
            $img->outertext = '&nbsp;&nbsp;<a href="https://www.youtube.com/results?search_query='. $movie /* Doesn't work, only displays one title, not one each of the 30*/ .'" target="_blank"><img src="img/youtube.png" alt="Trailer" title="Trailer" style="width:19px;" width="19" height="18" border="0"></a>';
        }
    }
}   
    
$html->save();
foreach($html->find("table") as $title) {
    echo $title->outertext . '<br>';
}
?>

ORIGINAL SOURCE:

<td>
  <div class="detName"> <a href="https://tpb.party/torrent/37614340/The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26" class="detLink" title="Details for The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26">The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26</a>
  </div>
  <a href="magnet:?xt=urn:btih:4AEE012597EBEA65840A96F62CEBE9926F8ECE5D&dn=The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2920%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.pirateparty.gr%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce"
    title="Download this torrent using magnet"><img src="https://tpb.party/static/img/icon-magnet.gif" alt="Magnet link" height="12" width="12"></a>
  <a href="https://tpb.party/user/sotnikam/"><img src="https://tpb.party/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border="0" height="11" width="11"></a><img src="https://tpb.party/static/img/11x11p.png" height="11" width="11">
  <font class="detDesc">Uploaded 11-27&nbsp;10:12, Size 2.71&nbsp;GiB, ULed by <a class="detDesc" href="https://tpb.party/user/sotnikam/" title="Browse sotnikam">sotnikam</a> </font>
</td>

How it’s now:

The HTML code that replaces the IMG elements and the problem being that the links are the same for ALL elements, when they should be Unique for each element like the MovieTitles:

<td>
  <div class="detName"> <a href="https://tpb.party/torrent/37614340/The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26" class="detLink" title="Details for The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26">The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26</a>
  </div>
  <a href="magnet:?xt=urn:btih:4AEE012597EBEA65840A96F62CEBE9926F8ECE5D&dn=The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2F9.rarbg.to%3A2920%2Fannounce&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=udp%3A%2F%2Ftracker.internetwarriors.net%3A1337%2Fannounce&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.pirateparty.gr%3A6969%2Fannounce&tr=udp%3A%2F%2Ftracker.cyberia.is%3A6969%2Fannounce"
    title="Download this torrent using magnet"><img src="https://tpb.party/static/img/icon-magnet.gif" alt="Magnet link" height="12" width="12"></a>
  <a href="https://tpb.party/user/sotnikam/"><img src="https://tpb.party/static/img/vip.gif" alt="VIP" title="VIP" style="width:11px;" border="0" height="11" width="11"></a>&nbsp;&nbsp;
  <a href="https://www.youtube.com/results?search_query=            The.Mandalorian.S02E05.Chapter.13.The.Jedi.2020.1080p.WEB-DL.X26  " target="_blank"><img src="img/youtube.png" alt="Trailer" title="Trailer" style="width:19px;" width="19" height="18" border="0"></a>
  <font class="detDesc">Uploaded 11-27&nbsp;10:12, Size 2.71&nbsp;GiB, ULed by <a class="detDesc" href="https://tpb.party/user/sotnikam/" title="Browse sotnikam">sotnikam</a> </font>
</td>

Advertisement

Answer

The image you want is nested in one of the siblings of the detName DIV. So you can search for it by searching within the parent element.

Since find() allows more complex CSS selectors, you can search specifically for the image you want, rather than looping through all the images.

foreach($html->find("div.detName") as $movieDiv) {
    $movie = $movieDiv->plaintext;
    echo $movie;    //Works Okey, it displays each of the movietitles
    
    $img = $movieDiv->parent()->find('img[src="https://tpb.party/static/img/11x11p.png"]', 0);
    if ($img) {
        $img->outertext = '&nbsp;&nbsp;<a href="https://www.youtube.com/results?search_query='. $movie .'" target="_blank"><img src="img/youtube.png" alt="Trailer" title="Trailer" style="width:19px;" width="19" height="18" border="0"></a>';
    }
}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement