Search code examples
phpextractsimple-html-dom

PHP HTML Simple DOM extract attribute


I have this html code :

<li class="ipsDataItem ipsDataItem_responsivePhoto ipsDataItem_unread  " data-rowID='79528'>
                <div class='ipsDataItem_icon ipsType_blendLinks'>
    <a href="https://www.nzbnewzfrance.ninja/profile/69-mcfly/" data-ipsHover data-ipsHover-target="https://www.nzbnewzfrance.ninja/profile/69-mcfly/?do=hovercard" class="ipsUserPhoto ipsUserPhoto_tiny" title="Aller sur le profil de McFly">
        <img src='https://www.nzbnewzfrance.ninja/uploads/monthly_2019_12/mcfly_avatart.thumb.gif.af84871aaa1d14550c54992f9249440a.gif' alt='McFly'>
    </a>

I'm looking to extract only "data-rowID" value, 79528. I made a lot of attempts like

foreach($html->find('li') as $f) {
   echo $f->getAttribute("data-rowID");
}

or

foreach($html->find('li[class=ipsDataItem ipsDataItem_responsivePhoto ipsDataItem_unread]') as $f) {
   echo $f->getAttribute("data-rowID");
}

but none is working. Where did I get it wrong ? Thank you.


Solution

  • You need to load the string into the parser via str_get_html(). The attribute name selector seems to be required to be lower case.

    $html = <<<'_HTML'
    <li class="ipsDataItem ipsDataItem_responsivePhoto ipsDataItem_unread  " data-rowID='79528'>
                    <li class='ipsDataItem_icon ipsType_blendLinks'>
        <a href="https://www.nzbnewzfrance.ninja/profile/69-mcfly/" data-ipsHover data-ipsHover-target="https://www.nzbnewzfrance.ninja/profile/69-mcfly/?do=hovercard" class="ipsUserPhoto ipsUserPhoto_tiny" title="Aller sur le profil de McFly">
            <img src='https://www.nzbnewzfrance.ninja/uploads/monthly_2019_12/mcfly_avatart.thumb.gif.af84871aaa1d14550c54992f9249440a.gif' alt='McFly'>
        </a>
    _HTML;
    
    $dom = str_get_html($html);
    foreach($dom->find('li') as $li) {
        echo $li->getAttribute('data-rowid');
    }
    

    Prints out

    79528