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

用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 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统
  • ¥15 关于wkernell.PDB加载的问题,如何解决?(语言-c#|开发工具-vscode)
  • ¥100 某宝多次访问被拒绝,求解
  • ¥15 (标签-STM32|关键词-智能小车)
  • ¥20 关于#stm32#的问题,请各位专家解答!