How to get the value of a tag with a specific class/attribute with DOM and php?

Asked

Viewed 181 times

0

Hello. It is that I wish to get the value of a certain tag with a specific class/ attribute using DOM and php. Below is the html that will be used:

           <div class="header nopadding filmesonline">
                <div class="letter"><i class="fa fa-film">Filmes para ver</i> FILMES</div>
                <div class="button hover active" data-type="movies" data-what="featured">LANÇAMENTOS</div>
                <div class="button hover" data-type="movies" data-what="recent">Recentes</div>
                <div class="button hover" data-type="movies" data-what="mostseen">Mais vistos</div>
                <div class="button hover" data-type="movies" data-what="recommended">Recomendados</div>
            </div>

php

<?php
$allAElements=$d->getElementsByTagName('div');
if ($allAElements){ 
    foreach ($allAElements as $elemento){
        $dataLoader= $elemento->getAttribute('data-loader'); 
        $dataWhat=$elemento->getAttribute('data-what');
        $class=$elemento->getAttribute('class');
        if($class==="header nopadding filmesonline"){
        if($class==="button hover active") $lancamento = $class->nodeValue; 
        var_dump($lancamento);
    }
} 
?>

And I want with DOM and php to get the values: Movies to see, Movies, RELEASES, More viewed and Recommended and save each one in a variable. HTML is much bigger but I would like to get these values that are in specific classes and tags.

I’d appreciate your help this is to do a job for the college.

  • You have tried something, studying some code that is finding it difficult..... http://php.net/manual/en/book.dom.php

  • Related: https://answall.com/questions/51643/diminur-valor-de-campo-span-com-preg-replace // https://answall.com/questions/13712/como-obten-a-tag-dentro-de-um-div-utilizando-xpath?rq=1

  • @Magichat I can’t use either regular expressions or xpath has even to be DOM.

  • @Magichat I can get the html I can’t get is with DOM get the resources I want.

  • I understand, but what are you trying to do? which error is getting... ?

  • I don’t get the values I want. I even wanted to send code that I have written but I signed up here today and I’m not able to send the code. You’re always saying you have too many characters

  • click [edit] and insert the code.... Just below the php html dom tags

  • $allAElements=$d->getelementsbytagname('div'); if ($allAElements){ foreach ($allAElements as $elemento){ $dataLoader= $elemento->getattribute('data-Loader'); $dataWhat=$element->getattribute('data-what'); $class=$element->getattribute('class'); if($class==="header nopadding filmesonline"){ if($class==="button Hover active") $lancing = $class->nodeValue; var_dump($lancamento); }}

  • I’m sorry to send so I don’t know why this is keeping me from sending because I have too many characters :( I don’t know if this happens to you too.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.