2012-06-29 54 views
0

我需要一些幫助請在這裏。試圖做的是拉出b節點中的所有內容。通過某些html標記集

<P><B>Credit Weighting: </B>5<BR><BR> 
<B>Teaching Period(s): </B>Teaching Periods 1 and 2.<BR><BR> 
<B>No. of Students: </B>-.<BR><BR> 
<B>Pre-requisite(s): </B>None<BR><BR> 
<P><A HREF="#top" class="toppage">[Top of page]</A></P> 

<P><B>Credit Weighting: </B>20<BR><BR> 
<B>Teaching Period(s): </B>Teaching Periods 1 and 2.<BR><BR> 
<B>No. of Students: </B>-.<BR><BR> 
<B>Pre-requisite(s): </B>None<BR><BR> 
<P><A HREF="#top" class="toppage">[Top of page]</A></P> 

<P><B>Credit Weighting: </B>10<BR><BR> 
<B>Teaching Period(s): </B>Teaching Periods 1 and 2.<BR><BR> 
<B>No. of Students: </B>-.<BR><BR> 
<B>Pre-requisite(s): </B>None<BR><BR> 
<P><A HREF="#top" class="toppage">[Top of page]</A></P> 

我能夠從第一set.Below拉的數據是我的示例代碼,這是否

// GETTING ALL THE B NODE STUFFS AND PRINTING IT'S CONTENTS 
    $result = array(); 
    foreach($document->getElementsByTagName('b') as $node){ 
    $result[preg_replace('/:\s+$/','',$node->textContent)] = trim($node->nextSibling->textContent); 
    } 
    var_dump($result); 
    echo '<br /><br />'; 

現在什麼我試圖做的是通過三組HTML代碼來獲得環所有的b節點,並得到的contets.Wow我可以去談論這個嗎?

回答

0

您的意思是這樣的?

$result = array(); 
$id= -1; 
foreach($document->getElementsByTagName('b') as $node){ 
    $field= preg_replace('/:\s+$/','',$node->textContent); 
    if ($field == "Credit Weighting") $id++; 
    $result[$id][$field]= trim($node->nextSibling->textContent); 
}. 
var_dump($result); 

這可以讓你:

array(3) { 
    [0] => 
    array(4) { 
    'Credit Weighting' => 
    string(1) "5" 
    'Teaching Period(s)' => 
    string(25) "Teaching Periods 1 and 2." 
    'No. of Students' => 
    string(2) "-." 
    'Pre-requisite(s)' => 
    string(4) "None" 
    } 
    [1] => 
    array(4) { 
    'Credit Weighting' => 
    string(2) "20" 
    'Teaching Period(s)' => 
    string(25) "Teaching Periods 1 and 2." 
    'No. of Students' => 
    string(2) "-." 
    'Pre-requisite(s)' => 
    string(4) "None" 
    } 
    [2] => 
    array(4) { 
    'Credit Weighting' => 
    string(2) "10" 
    'Teaching Period(s)' => 
    string(25) "Teaching Periods 1 and 2." 
    'No. of Students' => 
    string(2) "-." 
    'Pre-requisite(s)' => 
    string(4) "None" 
    } 
} 
+0

我不明白,那是什麼代碼呢,對吧?或者你的意思是平面陣列? – Searle

+0

是的,這就是我的意思。傳奇!謝謝,這很好 – user1444442

0

嘗試

preg_match_all("/\<B\>(.*)\<\/B>([^\<]+)/", $text, $regs); 

假設不必須在它的HTML標籤數據的第二位。

+0

對不起哪兒我會在上面的代碼符合這個正則表達式?以上所有內容都在html文件的主體內。 – user1444442