是这样的,我采集一点彩票开奖数据,
写了正则匹配,以经匹配出来了正确的结果数组($result)
但是呢,速度有一点慢,所以想请高手看看,有没有什么办法优化一下速度。代码入下:
$str = file_get_contents('http://baidu.lehecai.com/lottery/draw/?agentId=5555');
preg_match('/<table cellpadding="0" cellspacing="0" class="kj_tab">(.*)<\/table>/isU', $str, $strs);
preg_match_all('/(<tr>|<tr[^>]*>)(.*)<\/tr>/isU', $strs[0] , $output );
foreach($output[2] as $value){
preg_match_all('/(<td>|<td[^>]*>)(.*)<\/td>/isU',$value ,$va);
$result[] = $va[2];
}for($i = 2, $size =count($result); $i < $size ; $i++){
preg_match_all('/<a[^>]* href="([^"]*)"[^>]*>(.*)<\/a>/isU', $result[$i][0], $href);
preg_match_all('/(<a[^>]*>|<span[^>]*>)(.*)(<\/a>|<\/span>)/isU', $result[$i][3], $matches);
$result[$i][0] = "http://baidu.lehecai.com/".$href[1][0];
$result[$i][3] = $matches[2];
}//preg_match_all('/<a[^>]*>(.*)<\/a>/isU', $result[3][3][3], $matches); 
//$result[3][3][3] = preg_replace("/[\s]+/si","",$matches[1][0]);
$result[3][3][3] = preg_replace("/[\s]+/si","",$result[3][3][3]);
$result[3][3][3] = preg_replace("/\(.*\)/si","",$result[3][3][3]);
//echo "<pre>"; print_r($result);exit();/*
 * $result 说明   $result[$i][3] 为号码数 ; $result[$i][0] 为连接  ; $result[$i][1]  为开奖期数 ;
 * $i的说明如下表 :
 * 2:双色球
 * 3:福彩3D
 * 4:七乐彩
 * 5:华东15选5
 * 6:东方6+1
 * 8:大乐透
 * 9:排列三
 * 10:排列五
 * 11:七星彩
 * 12:22选5
 * 13:14场胜负彩
 * 14:任选9场
 * 15:4场进球彩
 * 16:6场半全场
 * 
 */

解决方案 »

  1.   

    我是匹配出这个接果后,前台用ajax get方法,传到服务段把要的数据传前台的,不知道是不是这个速度很慢,
    但是呢,我在本地测式的时候,有时候都会卡一下,本地应该不会存在get方法慢的原因吧。
      

  2.   

    估计是采集耽误时间。你可以把后面正则部分注释掉试试。
    当然ajax请求服务器也是需要时间来缓存的。
      

  3.   

    换 curl看看会不会好点,不过我猜应该不会有太大的区别.这不但跟你自身的网络cpu的处理能力还跟对方的网站相应速度有关联