用curl显示结果量

时间:2013-08-06 12:40:07

标签: php html curl html-table preg-match

是否可以仅显示收到卷曲的页面的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>';

?>

结果

  
    

Leather vambraces Leather vambraces免费游戏道具9 12 3 + 7%Bronze helm Bronze helm免费游戏产品53 72 19 + 6%Air rune Air     符文免费游戏项目22 30 8 + 6%Varrock teleport Varrock     传送会员&#39;只有项目969 1,255 286 + 5%传送到房子     传送到会员&#39;只有项目862 1,137 275 + 5%柚木原木柚木     记录会员&#39;只有83 111 28 + 5%Water orb Water orb会员&#39;     仅限项目1,491 1,930 439 +5%

  

(那只是一件通常有100个结果)

那么我有什么方法可以只显示4个结果吗?

~~~~~~~编辑~~~~~~

有没有办法从这里得到结果:&#34;

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']这样的变量中?

2 个答案:

答案 0 :(得分: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        

答案 1 :(得分: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>';