2013-06-20 64 views
0
<h2> 
<strong> 
<a href="http://www.linkedin.com/pub/rom-agustin/0/111/947" title="Rom Agustin"> 
    <span class="given-name">Rom</span> 
    <span class="family-name">Agustin</span> 
</a> 
</strong> 
</h2> 

所以我需要解析兩個span類並將它們存儲在一個變量中。使用DOM PHP解析子內容

span.given名= $ GIVEN_NAME

span.family名= $ FAMILY_NAME

現在我的代碼是:

foreach($vcard as $items): 
    $names = $items->getElementsByTagName('h2'); 
    $name = $names->item(0)->nodeValue; 
    echo $name; //Rom Agustin 
endforeach; 

如何正確地分離這兩個?或者我怎麼才能瞄準這個班呢?我已經閱讀了php.net中的DOM,但沒有GetElementbyClass。嘗試爆炸,非常混亂。

回答

0

使用http://simplehtmldom.sourceforge.net,這使得它很輕鬆,http://simplehtmldom.sourceforge.net/manual.htm#section_create解釋如何從純字符串工作。

$htmltext = "...your snippet..."; 
$html = str_get_html($htmltext); 
foreach($html->find("span") as $span) { 
    // use $span here! 
} 
+0

是否有任何其他方式從內置函數? :) –

+0

在我意識到的HTML解析中沒有好的內容。簡單的說就是下載文件,放入相同的目錄,包含文件。之後,你基本上正在用PHP進行jQuery調用,這非常方便。 –

+0

謝謝!將這個庫保持得心應手:) –