doulao2029 2015-07-19 06:04
浏览 31
已采纳

如何使用dom文件获取div类值

how to get div class value using dom document

i need to echo this value 4,458 members

from the below code

< div class="mbs fcg">4,458 members< /div>

right now my orginal code is

$links = $dom->getElementsByTagName('div');

foreach ($links as $link){

echo $link->nodeValue;
echo $link->getAttribute('class');

}

how to target this particular class = mbc fcg ?

now with my present code i am getting all div values.

what changes i should do

  • 写回答

3条回答 默认 最新

  • dp152153 2015-07-19 07:17
    关注

    you will need to use DOMXPath, which will take a DOMDocument instance

    $xpath   = new DOMXPath( $dom );
    // if the className doesn`t changes
    $members = $xpath->query( '//div[@class="mbs fcg"]' );
    // if the class name changes ex. class="mbs fcg my-other class-name"
    $members = $xpath->query( '//div[contains(@class,"mbs fcg")]' );
    

    alternatively if you want to iterate all over your div`s you could try

    $divs = $dom->getElementsByTagName( 'div' );
    foreach( $divs as $div ){
        // if the className doesn`t changes
        if( $div->getAttribute( 'class' ) === 'mbs fcg' ){
            echo $div->nodeValue;
        }
        // if the class name changes ex. class="mbs fcg my-other class-name"
        if( strpos( $div->getAttribute( 'class' ), 'mbs fcg' ) !== false ){
            echo $div->nodeValue;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)