2013-08-06 111 views
0

是否可以只顯示curl收到的頁面的4個結果?顯示捲曲結果量

這是我的腳本:

<?php 
$ch = curl_init ("http://services.runescape.com/m=itemdb_rs/top100.ws?list=2&scale=0"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$page = curl_exec($ch); 

preg_match('#<tbody[^>]*>(.+?)</tbody>#is', $page, $matches); 
foreach ($matches as &$match) { 
    $match = $match; 
} 
echo '<table>'; 
    echo $matches[0]; 
echo '</table>'; 

?> 

與此

皮革的結果臂鎧皮革臂鎧免費遊戲項目9 12 3 + 7%的青銅頭盔青銅頭盔免費遊戲項目53 72 19 + 6%空氣符文空氣 符文免費遊戲道具物品22 30 8 + 6%瓦爾羅克傳送瓦爾羅克 傳送會員唯一物品969 1,255 286 + 5%傳送到房間 傳送到會員只有我TEM 862 1137個275 + 5%柚木柚木 記錄員只有項目83 111 28 + 5%水寶珠水寶珠會員 唯一項目1491 1930 439 + 5%

(那只是一個和平通常有100個結果)

那麼有辦法我只能顯示4個結果嗎?

~~~~~~~編輯~~~~~~

有沒有辦法從這個地方的結果:」

Leather vambraces  9 12 3 +7% 
Bronze helm     53 72 19 +6% 
Air rune    22 30 8 +6% 
Varrock teleport  969 1,255 286 +5% 

這樣:

Leather vambraces   
Bronze helm     
Air rune     
Varrock teleport  

像$ item ['name']這樣的變量?

+3

史詩無用代碼:'的foreach { $匹配= $($爲&$匹配匹配)比賽; }'你是否爲我們更改過這個代碼,或者這實際上是你的代碼的作用? – SmokeyPHP

+0

@SmokeyPHP我沒有改變任何東西。只顯示少量總結果,否則該帖子會非常長 –

+0

在這種情況下,您可以刪除該foreach循環。它什麼都不做。 – SmokeyPHP

回答

2

試試這個(更新)

$ch = curl_init ("http://services.runescape.com/m=itemdb_rs/top100.ws?list=2&scale=0"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$page = curl_exec($ch); 

preg_match('#<tbody[^>]*>(.+?)</tbody>#is', $page, $matches); 
foreach ($matches as &$match) { 
    $match = $match; 
} 
     $dom = new DOMDocument(); 
     @$dom->loadHTML('<?xml encoding="UTF-8">' . $match); 
     $tables = $dom->getElementsByTagName('table'); 
     $thArray = $tdArray = $array = array(); 
     $tr = $dom->getElementsByTagName('tr'); 
     $i = 0; 
     $s = 0; 
     $k=5; 
     echo "table"; 
     foreach ($tr as $tr) 
     { 

      $thArray[] = $tr->nodeValue; 
      $td = $dom->getElementsByTagName('td'); 
      ini_set('max_execution_time', 99999999999999999999); 
      foreach($td as $td) 
      { 
       ini_set('max_execution_time', 99999999999999999999); 
       $thAr[] = $td->nodeValue;   
        if($s<=$k) 
        { 
         $thArrays[$i][] =$thAr[$s]; 
         $n = $s; 
         $t=$s+6; 
        } 
        $s++;    
      } 

       $s= $n+1; 
       $k=$t; 

       $i++; 


     } 
     echo "<table>"; 
     for($i=0;$i<4;$i++) 
     { 
     echo "<tr>"; 
     $row = $thArrays[$i]; 
     $ks = count($row); 

     for($k=0;$k<1;$k++) 
     { 
      echo "<td>"; 
      echo $row[$k]; 
      echo "</td>";   
     } 
     echo "</tr>";  
     } 
     echo "</table>"; 
     unset($thArray); 
      unset($thArrays); 

     exit; 

輸出這樣的格式

Leather vambraces   
Bronze helm   
Air rune     
Varrock teleport   
+0

嘗試它,但我仍然得到所有結果 –

+0

@Jacob Brol現在檢查它 –

+0

給我一個空白頁:( –

0

很肯定這你想要做什麼:

preg_match_all('#<tr[^>]*>(.+?)</tr>#is', $page, $matches); 
$items = array(); 
unset($matches[0][0]); 
foreach ($matches[0] as $match) { 
    $items[] = $match; 
} 
$items = array_slice($items,0,4); //Get first 4 
echo '<table>'; 
    foreach($items as $item) 
    { 
     echo $item; 
    } 
echo '</table>';