dongshi1880 2017-09-21 02:23
浏览 144
已采纳

HTML dom解析器获取内部h5数据

<div class="username-info-area">
                <img src="https://image5.sahibinden.com/users/47/06/46/p200_profile_14470646_8126809.png" class="user-profile-photo" height="50" width="50" alt="">
                <h5>familya yapı Gayrimenkul Gyo ltd şti</h5>
                </div>

I want to get h5 data. I can access div.username-info-area.

$items = $new_data->find('div.username-info-area', 0)->outertext; 
        echo $items . '<br>';
  • 写回答

1条回答 默认 最新

  • dongzaotiao2863 2017-09-21 02:29
    关注

    Run an if clause inside of your foreach loop, check this example out for instance:

    The script below will make sure that if there's an h5 value it will print $div and its outertext.

    <?php
    foreach ($new_data->find('div[class=username-info-area]') as $div)
    {
        if($div->find('h5') !== '')
        {
            print_r($div->outertext);
        }
    }
    ?>
    

    Or you can run another foreach loop inside of the next foreach loop, for example.

        <?php
    foreach ($new_data->find('div[class=username-info-area]') as $div)
    {
        foreach($div->find('h5') as $h5)
        {
            print_r($h5->outertext);
        }
    }
    ?>
    

    I hope this help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?