How do I get this text using PHP Simple HTML dom?

时间:2015-10-30 23:20:29

标签: php html web-scraping simple-html-dom

Assuming I have this HTML Code in an HTML source:

<div class="info">
    <p><span class="strong">Score</span>4.32/5</p>
    <p><span class="strong">Type</span>TV</p>
    <p><span class="strong">Episodes</span>13</p>
    <p><span class="strong">Status</span>Ongoing</p>
    <p><span class="strong">Aired</span>Oct 3, 2015 - Dec 26, 2015</p>
    <p><span class="strong">Age</span>PG-13 - Teens 13 or older</p>
</div>

How do I use the PHP Simple HTML dom to get the text 4.32 from above? I tried

foreach($info_html->find('.strong') as $library) {
    $rating = $library->innertext;
    var_dump($rating);
}

But that only gives me the text "score", but I want 4.32.

2 个答案:

答案 0 :(得分:0)

也许这会给你你想要的结果:

foreach($info_html->find('div.info p') as $library) {
    $rating = $library->find('text', 1)->innertext;
    var_dump($rating);
}

答案 1 :(得分:0)

或者你可以找到&lt; p>首先标记元素:

foreach($info_html->find('p') as $library) {
        $rating = $library->find('text', 1)->plaintext;
        var_dump($rating);
}