2013-03-18 75 views
1

我正在尋找從外部URL收集信息,並將其剝離爲值。PHP Dom解析器來獲得跨度值

例如

<span id="ctl00_cphRoblox_rbxUserStatisticsPane_lFriendsStatistics">149</span> 

我不能找到一種方法,使用PHP DOM獲得 '149'

請幫幫忙,謝謝!

回答

1

一個辦法解決辦法是使用preg_match(),但我只將與捲曲()使用它...

$row = '<span id="ctl00_cphRoblox_rbxUserStatisticsPane_lFriendsStatistics">149</span>'; 
preg_match_all('/<span.*?>.*?<\/[\s]*span>/s', $row, $matches2); 
var_dump($matches2); 

另一種選擇是使用simple_html_dom.php:

include('simple_html_dom.php'); 
$html = str_get_html($row); 

var_dump($html->find('span', 0)->plaintext); 

第三個是使用內置的DOMDocument。

0
function DOMRemove(DOMNode $from) { 
    $sibling = $from->firstChild; 
    do { 
     $next = $sibling->nextSibling; 
     $from->parentNode->insertBefore($sibling, $from); 
    } while ($sibling = $next); 
    $from->parentNode->removeChild($from);  
} 

$dom = new DOMDocument; 
$dom->load('test.html'); 

$nodes = $dom->getElementsByTagName('span'); 
foreach ($nodes as $node) { 
    DOMRemove($node); 
} 
echo $dom->saveHTML(); 

來源:https://stackoverflow.com/a/4663865/1675369