/**
* 功能:解析数据
* 格式: array('timestamp','mid','data');
* @param string $data
* @return boolean|array
*/
private function _parseData($data)
{
if(empty($data) || !is_string($data)) return false;
$result = array(
'timestamp'=>0,
'mid'=>0,
'data'=>array(),
);
$data = explode('|', $data);
if(count($data) < 3 || !is_array($data)) return false;
$result['timestamp'] = $data[0];
$result['mid'] = $data[1];
$result['data'] = @json_decode($data[2] , true);
if(empty($result['timestamp']) || empty($result['mid'])) return false;
unset($data);
return $result;
}
/**
* TODO:读取单一文件
* @param string $date: 如(20140327)
* @param int $appid: 如(1000,9000)
* @param string $op:如(show,login , index)
* @param number $startNum 默认从第一行开始
* @param number $endNum 默认到$this->limit结束
* @param array $condition:array('mid'=>arrray() , 'ip'=>array() , ...) 过滤条件
* @param bool $jsonFlag:默认为true, 则保留jsondata字段;设为false,则去掉false字段
* @return array(count , diffTime , memory , data)
*/
public function readFile($date , $appid , $op ,$startNum=0 , $endNum=0 , $jsonFlag=false , $condition=array())
{
$data['data'] = "";
$data['count'] = 0;
$index = $startNum;
$startNum = empty($startNum) ? 0 : $startNum;
$endNum = empty($endNum) ? $this->limit : $endNum;
$hander = self::_openFile($date , $appid , $op );
$tmpindex=0;
if($hander)
{
//!feof($hander)
while ($tmpindex < $endNum)
{
$tmp = fgets($hander , $this->length);
if(empty($tmp)) continue;
if($tmpindex < $endNum && $tmpindex >=$startNum)
{
$tmp = self::_parseData($tmp);
if(empty($tmp)) continue;
//去掉jsondata
if(!$jsonFlag) { unset($tmp[2]); $condition= array(); }
//条件过滤
if($condition && is_array($condition) )
{
foreach ($condition as $key=>$val){
if(in_array($tmp['data'][$key], $condition[$key]))
unset($key , $val);
$data['count']++;
$data['data'][$index] = $tmp;
$index++;
}
}
else{
$data['data'][$index] = $tmp;
$index++;
$data['count']++;
}
}
if($tmpindex >= $endNum) break;
$tmpindex++;
unset($tmp);
}
fseek($hander , SEEK_END);
}
$data['diffTime'] = doubleval(microtime(true)) - doubleval($this->startTime);
$data['memory'] = doubleval((doubleval(memory_get_usage(true)) - doubleval(self::$startMemory))/1024/1024) . ' M';
return $data;
}
php读取大文件示例分享(文件操作类)(5)
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.heiqu.com/2702c6927e7b1112b19e7da1da893478.html