<?php
define('HD_V', 'V1.2');
function huoduan_get_baidu_top(){
    $file = ROOT_PATH.'/data/huoduan.baidutop.php';$c=a(a);
if(is_file($file) && time()-filemtime($file)<7200){
include($file);
}else{
$baiduurl = 'http://top.baidu.com/buzz.php?p=top10';
$html = huoduan_get_html($baiduurl);

$list = huoduan_get_content_array($html,'fr=top1000&amp;w'.i($c,7,1).'=','"',1);
$toplist = array_flip(array_flip($list));

if(is_array($toplist)){
  foreach($toplist as $k=>$v){
  $v = iconv("GBK","utf-8",urldecode($v));
  
  if(strlen($v)>9){
  $v = str_replace('"','',$v);
  $v = str_replace("'",'',$v);
  }
  $topkey[] = $v;
  
  }
}
if(count($topkey)>10){
   file_put_contents($file,"<?php\n \$topkey =  ".var_export($topkey,true).";\n?>");
}
}
return $topkey;
}
function huoduan_get_baidu($q,$p=1,$time=86400){
$s = urlencode($q);
$file = ROOT_PATH.'/cache/so_'.md5($q.$p).'.php';
$list = '';
if(is_file($file) && time()-filemtime($file)<$time){
include($file);
}else{     $html = huoduan_get_html('http://www.baidu.com/s?wd='.$s.'&pn='.(($p-1)*10).'&rn=15&tn=baidulocal&ie=utf-8');

//$html = iconv("GBK","utf-8",$html);
if(!strpos($html,'未找到和您的查询"<font')){ $body = huoduan_get_body($html,'<ol>','</ol>',1);
   
$lists['title'] = huoduan_get_content_array($body,'<td class=f>','<br>',1);

$lists['des'] = huoduan_get_content_array($body,'<font size=-'.substr(a(a),-4,1).'>','<br>',1);
$lists['blink'] = huoduan_get_content_array($body,'<font color=#008000>','</font>',1);

foreach($lists['title'] as $k=>$v){
$list['data'][$k]['title'] = huoduan_get_body($lists['title'][$k],'<font size="3">','</font></a>',1);
$list['data'][$k]['title'] = str_replace('<font color="#c60a00">','<em>',$list['data'][$k]['title']);
$list['data'][$k]['title'] = str_replace('</font>','</em>',$list['data'][$k]['title']);

$list['data'][$k]['link'] = huoduan_get_body($lists['title'][$k],'<a href="','"',1);

$list['data'][$k]['des'] = strip_tags($lists['des'][$k],'<font>');
$list['data'][$k]['blink'] = $lists['blink'][$k];


}

  $pager = huoduan_get_body($html,'</ol><ol>','</ol>',1);
  $pagerli = huoduan_get_content_array($pager,'<a href="','</a>',0);
 
  if(strpos($pager,'下一页')){
 $pcount = count($pagerli);
 $list['pnum'] = strip_tags($pagerli[$pcount-i(a(a),-12,1)]);
 $list['pnum'] = trim($list['pnum'],'[');
 $list['pnum'] = trim($list['pnum'],']');
 $list['pnext']=1;
  }else if(is_array($pagerli)){
  $pcount = count($pagerli);
  $list['pnum'] = strip_tags($pagerli[$pcount-i(a,112,1)]);
  $list['pnum'] = trim($list['pnum'],'[');
  $list['pnum'] = trim($list['pnum'],']');
  $list['pnext']=0;
  } if(is_array($list['data'])){
file_put_contents($file,"<?php\n \$list =  ".var_export($list,true).";\n?>");
}
}
}
return $list;
}function huoduan_get_baidu_xg($q){
$s = urlencode($q);
$file = ROOT_PATH.'/cache/xg_'.md5($q).'.php';
if(is_file($file)){
include($file);
}else{     $html = huoduan_get_html('http://www.baidu.com/s?wd='.$s.'&ie=utf-8');

//$html = iconv("GBK","utf-8",$html);
if(strpos($html,'相关搜索</th>')){ $body = huoduan_get_body($html,'相关搜索</th>','</table>',1);
   
$xglist = huoduan_get_content_array($body,'<'.substr(a,64,1),'</a>',0);
        foreach($xglist as $k=>$v){
$xgdata[$k] = strip_tags($v);
}
file_put_contents($file,"<?php\n \$xgdata =  ".var_export($xgdata,true).";\n?>");

}
}
return $xgdata;
}
function huoduan_get_body($str,$start,$end,$option){
  $strarr=explode($start,$str);
  $tem=$strarr[1];
  if(empty($end)){
  return $tem;
  }else{
  $strarr=explode($end,$tem);
  if($option==1){
  return $strarr[0];
  }
  if($option==2){
  return $start.$strarr[0];
  }
  if($option==3){
  return $strarr[0].$end;
  }
  else{
  return $start.$strarr[0].$end;
  }
  }
    }

function huoduan_replace_content($str,$start,$end,$replace = '',$option){
$del_code = huoduan_get_body($str,$start,$end,$option);

$str = str_replace( $del_code, $replace, $str );
return $str;

}

function huoduan_zz($string){
 $string = str_replace( '/', '\/', $string );
 $string = str_replace( '$', '\$', $string );
 $string = str_replace( '*', '\*', $string );
 $string = str_replace( '"', '\"', $string );
 $string = str_replace( "'", "\'", $string );
 $string = str_replace( '+', '\+', $string );
 $string = str_replace( '^', '\^', $string );
 $string = str_replace( '[', '\[', $string );
 $string = str_replace( ']', '\]', $string );
 $string = str_replace( '|', '\|', $string );
 $string = str_replace( '{', '\{', $string );
 $string = str_replace( '}', '\}', $string );
 $string = str_replace( '%', '\%', $string );
 $string = str_replace( '-', '\-', $string );
 $string = str_replace( '(', '\(', $string );
 $string = str_replace( ')', '\)', $string );
 $string = str_replace( '>', '\>', $string );
 $string = str_replace( '<', '\<', $string );
 $string = str_replace( '?', '\?', $string );
 $string = str_replace( '.', '\.', $string );
 $string = str_replace( '!', '\!', $string );
 return $string;
  }

function huoduan_get_content_array($str,$start,$end,$option){
$start_h = huoduan_zz($start);
$end_h = huoduan_zz($end);
    preg_match_all('/'.$start_h.'(.+?)'.$end_h.'/is',$str,$match);
  
$count = count($match[1]);
for($i=0;$i<$count;$i++){

  if($option==1){
     $arr[$i]=$match[1][$i];
  }
  else if($option==2){
     $arr[$i]=$start.$match[1][$i];
  }
  else if($option==3){
  $arr[$i]=$match[1][$i].$end;
  }else{
      $arr[$i]=$start.$match[1][$i].$end;
  }
}
return $arr;
}