0

I use the following code to cut out a div from a site.

$url = 'http://mypage.com/index.php';
$html = @file_get_html($url);
$GetScore = $html->find('div[class=ismSBValue]',0);
echo $GetScore;

This returns.....

<div class="ismSBValue">
    <div>   58
        <sub>pts</sub>                                     
    </div>
</div>

How do I cut out ONLY the 58 value?

I can do the following...

$GetScore = $html
 ->find('div[class=ismSBValue]',0)
  ->find('div',0)
   ->innertext;

to get it down to this....

   58
    <sub>pts</sub>

Is there some way to exclude the <sub> tag using simple_html_dom? or am I stuck to using str_replace() or strpos() to cut it further?

4

2 回答 2

1

不要删除sub,您以后可能需要它。一个简单的正则表达式将起作用:

preg_match('/\d+/', $html->find('div.ismSBValue div', 0)->text(), $match);
echo($match[0]); // 58
于 2013-05-04T00:34:43.720 回答
1

克隆然后删除你不想要的

JAVASCRIPT

$(function(){
var a = $('.ismSBValue').clone();
    a.find('sub').remove();
        console.log($.trim(a.text()));
})

PHP

$html = str_get_html('<div class="ismSBValue">
    <div>   58
        <sub>pts</sub>                                     
    </div>
</div>');


$html->find('div[class=ismSBValue]',0)->find('div',0)->find('sub',0)->innertext = '';
$a = $html->find('div[class=ismSBValue]',0)->plaintext;
echo trim($a); //58
于 2013-05-03T03:09:43.937 回答