2013-02-01 81 views
0

我嘗試從HTML頁面的外部源提取4G Network的實際是LTE 700 MHz Class 17/1700/2100 - for AT&T的整個行,但在其他情況下,它可能會更多不同,如LTE 850/900/1700/2100或其他。從外部HTML閱讀基於類

標題我的結果始終是穩定的(4G Network),它是<td class="ttl">下,結果是下<td class="nfo">類,但他們兩人都<tr>下,所以我認爲基於標題ttl class可以讀取nfo class下內容創建

這是外部的HTML源:

<tr> 
<th rowspan="8" scope="row">General</th> 
<td class="ttl"><a href="network-bands.php3">2G Network</a></td> 
<td class="nfo">CDMA 800/1900 </td> 
</tr><tr> 
<td class="ttl">&nbsp;</td> 
<td class="nfo">GSM 900/1800/1900 </td> 
</tr> 
<tr> 
<td class="ttl"><a href="network-bands.php3">3G Network</a></td> 
<td class="nfo">HSDPA 2100 </td> 
</tr> 
<tr> 
<td class="ttl">&nbsp;</td> 
<td class="nfo">CDMA2000 1xEV-DO </td> 
</tr> 
<tr> 
<td class="ttl"><a href="network-bands.php3">4G Network</a></td> 
<td class="nfo">LTE 700 MHz Class 17/1700/2100 - for AT&amp;T</td> 
</tr><tr> 
<td class="ttl"><a href="glossary.php3?term=sim">SIM</a></td> 
<td class="nfo">Micro-SIM</td> 
</tr><tr> 
<td class="ttl"><a href="#" onclick="helpW('h_year.htm');">Announced</a></td> 
<td class="nfo">2012, October</td> 
</tr> 

這是我使用的代碼:

<?php 
include_once('/simple_html_dom.php'); 
$dom = file_get_html("http://www.externalsite.com/pantech_vega_no_6-5268.php"); 
// alternatively use str_get_html($html) if you have the html string already... 
foreach ($dom->find('td[class=nfo]') as $node) 
{ 
$result = $node->innertext; 
$bresult = explode(",", $result); 
echo $bresult[0]; 
} 
?> 

而且我的代碼的結果是這樣的:

CDMA 800/1900 GSM 900/1800/1900 HSDPA 2100 CDMA2000 1xEV-DO LTE 700 MHz Class 17/1700/2100 - for AT&T Micro-SIM 2012, October

回答

1

如果你只是想獲得4G網絡,你應該做這樣的:

<?php 
include_once('/simple_html_dom.php'); 
$dom = file_get_html("http://www.gsmarena.com/pantech_vega_no_6-5268.php"); 
foreach ($dom->find('tr') as $node) { 
    if (is_a($node->children(0), 'simple_html_dom_node')) { 
     if ($node->children(0)->plaintext == "4G Network") { 
      echo $node->children(1)->plaintext; 
     } 
    } 
} 
?> 
+1

是的是的,很好的代碼Danilo,謝謝你很好,我apreciate! –

0

你通過所有的結果,而循環您只想要第一個。所以,不要循環,得到的第一個結果是:

$results = $dom->find('td[class=nfo]'); 
$node = reset($results); // get the first element of the array 
//the rest of your code: 
$result = $node->innertext; 
// etc. 
+0

我編輯的問題,所以我的結果是不是第一個元素'NFO class' –

0

你只想在第一個結果,所以你可以阻止你發現後,和你的結果

foreach ($dom->find('td[class=nfo]') as $node){ 
    $result = $node->innertext; 
    break; 
} 
+0

的外部來源是不完整的,上層是其他'TD class'文本,我將編輯該問題。 –