2011-06-18 82 views
1

我有一個腳本,它使用cURL從站點中獲取頁面,而im試圖逐行剪切它,然後提取我需要的內容並刪除該行。問題是,有時它會給我一個項目的正確位置,有時它不會取決於哪一行,它交替。PHP strpos問題

我嘗試使用這些頁面之一:使用http://www.gametracker.com/search/

代碼IM是:

while (strpos($contents, '<tr>')) 
{ 
    $start = strpos($contents, '<tr>'); 
    $end = strpos($contents, '</tr>', $start) + 5; 
    $rstring = substr($contents, $start, $end - $start); 
    $contents = str_replace($rstring, '', $contents); 
    //nameee 
     $start = strpos($rstring, '[clantag]'); 
    $end = strpos($rstring, '</a>', $start); 
    $name= trim(substr($rstring, $start, $end - $start)); 
    echo $name . '<br/>'; 
     //players 
    $search = '<td class="c05 item_bgcolor_even">'; 
     $start = (strpos($rstring, $search) + strlen($search)); 
    $end = strpos($rstring, "</td>", $start); 
    $players= trim(substr($rstring, $start, ($end - $start))); 
    echo $players . '<br />'; 
    $map; 
    $curPlayers; 
    $maxPlayers; 
    $ip; 
} 

它正確地獲取服務器的名字,但是當涉及到的球員,一個行我讓他們和其他的我得到lor_odd「> 1230

回答

1

這將是一個DOM parser比較容易的方式:

libxml_use_internal_errors(true); 

$dom = new DOMDocument; 
$dom->loadHTMLFile('http://www.gametracker.com/search/'); 
$xpath = new DOMXPath($dom); 

// Get the rows from the search list 
$rows = $xpath->query('//table[@class="table_lst table_lst_srs"]/tr'); 

for ($i=1; $i<$rows->length-1; $i++) { 
    $row = $rows->item($i); 

    // Get the columns for a row 
    $cols = $row->getElementsByTagName('td'); 

    // Get the server name (3rd column) 
    echo 'Name: '.trim($cols->item(2)->textContent).PHP_EOL; 

    // Get the player count (5th column) 
    echo 'Players: '.trim($cols->item(4)->textContent).PHP_EOL.PHP_EOL; 
} 

輸出:

Name: NoobonicPlague.com | 24/7 RP, FastDL, Custom Mods 
Players: 55/65 

Name: RUS Coop-20 SemantiC#1 (2.0.7.0) 
Players: 20/20 

Name: TAW.net -(Dallas) 
Players: 32/32 

Name: Dedicated 
Players: 0/12 

... 
+0

謝謝,這正是我需要的。我不知道它可以以一種更容易的方式完成。 – Jonathan

1

你一直在寫‘錯誤’之類的這樣的處理代碼。這是很難理解和維護,更不用說調試。

如果你願意放棄手動HTML解析和使用XPath查詢相反,你的生活將變得簡單:

$doc = new DOMDocument(); 
$doc->loadHTML($contents); // $contents = the HTML that curl returns 

$xpath = new DOMXPath($doc); 

// You can now use as many XPath queries as you like; they look like this: 
$nodes = $xpath->query(''); // you own query expression here 
foreach ($nodes as $node) { 
    $v = $node->nodeValue; // now do something with $v 
} 
+0

是的,我知道這很難理解,但我只是測試看看我是否真的可以挖掘出我需要的所有東西,但是使用dom解析器卻變得容易得多。謝謝。 – Jonathan