2013-03-18 80 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