PHP HTML 简单 DOM 提取属性

PHP HTML Simple DOM extract attribute

我有这个 html 代码:

<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>

我只想提取“data-rowID”值 79528。我做了很多尝试,比如

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

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

但 none 正常。我哪里弄错了?谢谢。

您需要通过 str_get_html() 将字符串加载到解析器中。属性名称选择器似乎需要小写。

$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');
}

打印出来

79528