2013-04-27 69 views
3

有沒有辦法抓取部分匹配的ID的所有元素。例如,如果我想抓取網頁上的所有HTML元素,並且id屬性以msg_開頭,但可能是之後的任何內容。PHP DomDocument - getElementByID(部分匹配)如何?

這是我走這麼遠:

$doc = new DomDocument; 

// We need to validate our document before refering to the id 
$doc->validateOnParse = true; 
$doc->loadHtml(file_get_contents('{URL IS HERE}')); 
foreach($doc->getElementById('msg_') as $element) { 
    foreach($element->getElementsByTagName('a') as $link) 
    { 
     echo $link->nodeValue . "\n"; 
    } 
} 

但我需要弄清楚如何做一個局部的ID匹配與該位:$doc->getElementById('msg_')或者是否有其他方式來做到這一點...?

基本上,我需要抓住所有「A」標記,是從1開始msg_ id爲元素的兒童技術上總是有,只是,將是1個a標籤,但我不知道該怎麼隨便抓第一個孩子,這就是爲什麼我也使用foreach。

這是可能與DomDocument PHP類?

這是我現在使用的代碼,它不工作,要麼:

$str = ''; 
$filename = 'http://dream-portal.net/index.php/board,65.0.html'; 
@set_time_limit(0); 

$fp = fopen($filename, 'rb'); 
while (!feof($fp)) 
{ 
    $str .= fgets($fp, 16384); 
} 
fclose($fp); 

$doc = new DOMDocument(); 
$doc->loadXML($str); 

$selector = new DOMXPath($doc); 

$elements = $selector->query('//row[starts-with(@id, "msg_")]'); 

foreach ($elements as $node) { 
    var_dump($node->nodeValue) . PHP_EOL; 
} 

HTML如下(它在span標籤):

<td class="subject windowbg2"> 
<div> 
    <span id="msg_6555"> 
    <a href="http://dream-portal.net/index.php?topic=834.0">Poll 1.0</a> 
    </span> 
    <p> 
    Started by 
    <a href="http://dream-portal.net/index.php?action=profile;u=1" title="View the profile of SoLoGHoST">SoLoGHoST</a> 
    <small id="pages6555"> 
     « 
     <a class="navPages" href="http://dream-portal.net/index.php?topic=834.0">1</a> 
     <a class="navPages" href="http://dream-portal.net/index.php?topic=834.15">2</a> 
     » 
    </small> 

         with 963 Views 

    </p> 
</div> 
</td> 

這是<span id="msg_部分,還有一堆(HTML頁面上至少有15個)。

+0

不要用的loadXML()加載HTML,使用loadHTML() – 2013-04-27 04:30:23

+0

謝謝,這似乎已經擺脫了錯誤的,然而,仍然是空白... – 2013-04-27 04:35:48

+0

然後,我會建議張貼一部分的HTML以及。 – 2013-04-27 04:37:55

回答

4

使用此:

$str = file_get_contents('http://dream-portal.net/index.php/board,65.0.html'); 

$doc = new DOMDocument(); 
@$doc->loadHTML($str); 

$selector = new DOMXPath($doc); 

foreach ($selector->query('//*[starts-with(@id, "msg_")]') as $node) { 
    var_dump($node->nodeValue) . PHP_EOL; 
} 

爲您提供:

string(8) "Poll 1.0" 
string(12) "Shoutbox 2.2" 
string(24) "Polaroid Attachments 1.6" 
string(24) "Featured News Slider 1.3" 
string(17) "Image Resizer 1.0" 
string(8) "Blog 2.2" 
string(13) "RSS Feeds 1.0" 
string(19) "Adspace Manager 1.2" 
string(21) "Facebook Like Box 1.0" 
string(15) "Price Table 1.0" 
string(13) "SMF Links 1.0" 
string(19) "Download System 1.2" 
string(16) "[*]Site News 1.0" 
string(12) "Calendar 1.3" 
string(16) "Page Peel Ad 1.1" 
string(20) "Sexy Bookmarks 1.0.1" 
string(15) "Forum Staff 1.2" 
string(21) "Facebook Comments 1.0" 
string(15) "Attachments 1.4" 
string(25) "YouTube Channels 0.9 Beta" 
+0

是的,但我如何從一個URL路徑加載一個外部HTML文件到'XPath' ??我可以這樣做:'$ doc-> loadXML(file_get_contents('{URL IS HERE}'));'但是我需要將HTML轉換成XML嗎? – 2013-04-27 03:31:33

+0

使用'$ str = file_get_contents($ your_url);' – hek2mgl 2013-04-27 03:32:16

+0

這不起作用,測試過它,我什麼也沒得到,只是空白的內容 – 2013-04-27 03:43:10