dongxian4531 2013-08-06 12:40
浏览 38
已采纳

用curl显示结果量

Is it possible to only show like 4 results from a page received with curl?

This is my script :

<?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>';

?>

with a result of this

Leather vambraces Leather vambraces Free game item 9 12 3 +7% Bronze helm Bronze helm Free game item 53 72 19 +6% Air rune Air rune Free game item 22 30 8 +6% Varrock teleport Varrock teleport Members' only item 969 1,255 286 +5% Teleport to house Teleport to house Members' only item 862 1,137 275 +5% Teak logs Teak logs Members' only item 83 111 28 +5% Water orb Water orb Members' only item 1,491 1,930 439 +5%

(That's just a piece normally there are like 100 results)

So is there way that i can only like display 4 results?

~~~~~~~Edit~~~~~~

Is there a way to place the result from this :"

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% 

to this :

Leather vambraces        
Bronze helm                  
Air rune                
Varrock teleport     

In a variable like $item['name']?

  • 写回答

2条回答 默认 最新

  • doufang8282 2013-08-06 12:55
    关注

    Try this (Updated)

     $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;
    

    Output like this format

    Leather vambraces        
    Bronze helm         
    Air rune                
    Varrock teleport        
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址