简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM