如何使用PHP 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.

假设我在HTML源代码中有这个HTML代码: p>

 &lt; div class =“info”&gt; 
&lt; p&gt;&lt; span class =“strong”&gt;得分&lt; / span&gt; 4.32 / 5&lt; / p&gt; 
&lt; p&gt;&lt; span class =  “strong”&gt;类型&lt; / span&gt;电视&lt; / p&gt; 
&lt; p&gt;&lt; span class =“strong”&gt;集数&lt; / span&gt; 13&lt; / p&gt; 
&lt; p&gt;&lt; span  class =“strong”&gt;状态&lt; / span&gt;正在进行&lt; / p&gt; 
&lt; p&gt;&lt; span class =“strong”&gt;已播出&lt; / span&gt; 2015年10月3日 -  2015年12月26日&lt; / p&gt  ; 
&lt; p&gt;&lt; span class =“strong”&gt;年龄&lt; / span&gt; PG-13  - 青少年13岁或以上&lt; / p&gt; 
&lt; / div&gt; 
  code>  pre>  
 
 

如何使用PHP Simple HTML dom从上面获取文本4.32? 我试过 p>

  foreach($ info_html-&gt; find(  '.strong')作为$ library){
 $ rating = $ library-&gt; innertext; 
 var_dump($ rating); 
} 
  code>  pre> 
 
 

但这只给了我文本“得分”,但我想要4.32。 p> div>

Maybe this will give you the result you are looking for:

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

Or you can find < p > tag elements first:

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