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条)

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站