1、curl模拟HTTP/HTTPS,支持GET/POST
protected function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
//参数不为空则为post请求
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
2、判断是否为手机端访问
/**
* 判断是否为手机端访问
* @return boolean
*/
function isMobile()
{
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
$clientkeywords = array(
'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-'
, 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini',
'operamobi', 'opera mobi', 'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile'
);
// 从HTTP_USER_AGENT中查找手机浏览器的关键字
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", $userAgent) && strpos($userAgent, 'ipad') === false) {
return true;
}
}
return false;
}
3、判断是否为搜索引擎蜘蛛
/**
* 判断是否为搜索引擎蜘蛛
* @return string
*/
function is_spider($record = true)
{
static $spider = NULL;
if ($spider !== NULL)
{
return $spider;
}
if (empty($_SERVER['HTTP_USER_AGENT']))
{
$spider = '';
return '';
}
$searchengine_bot = array(
'googlebot',
'mediapartners-google',
'baiduspider+',
'msnbot',
'yodaobot',
'yahoo! slurp;',
'yahoo! slurp china;',
'iaskspider',
'sogou web spider',
'sogou push spider'
);
$searchengine_name = array(
'GOOGLE',
'GOOGLE ADSENSE',
'BAIDU',
'MSN',
'YODAO',
'YAHOO',
'Yahoo China',
'IASK',
'SOGOU',
'SOGOU'
);
$spider = strtolower($_SERVER['HTTP_USER_AGENT']);
foreach ($searchengine_bot AS $key => $value)
{
if (strpos($spider, $value) !== false)
{
$spider = $searchengine_name[$key];
if ($record === true)
{
$GLOBALS['db']->autoReplace($GLOBALS['ecs']->table('searchengine'), array('date' => local_date('Y-m-d'), 'searchengine' => $spider, 'count' => 1), array('count' => 1));
}
return $spider;
}
}
$spider = '';
return '';
}
4、递归方式的对变量中的特殊字符进行转义
/* 对用户传入的变量进行转义操作。*/
if (!get_magic_quotes_gpc())
{
if (!empty($_GET))
{
$_GET = addslashes_deep($_GET);
}
if (!empty($_POST))
{
$_POST = addslashes_deep($_POST);
}
$_COOKIE = addslashes_deep($_COOKIE);
$_REQUEST = addslashes_deep($_REQUEST);
}
/**
* 递归方式的对变量中的特殊字符进行转义
* @return mix
*/
function addslashes_deep($value)
{
if (empty($value))
{
return $value;
}
else
{
return is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
}
}
5、递归打印目录
/**
*递归打印目录
*/
function print_dir($path,$lev=1){
$dh=opendir($path);
while($str=readdir($dh)){
echo str_repeat(' ',$lev).$str.'
';
if($str=='.'||$str=='..'){
continue;
}
if(is_dir($path.'/'.$str)){
print_dir($path.'/'.$str,$lev+2);
}
}
closedir($path);
}
6、UTF-8、gb2312都支持的汉字截取函数
/*
Utf-8、gb2312都支持的汉字截取函数
cut_str(字符串, 截取长度, 开始长度, 编码);
编码默认为 utf-8
开始长度默认为 0
*/
function StrCut($string,$sublen, $start = 0,$code='UTF-8')
{
if($code=='UTF-8') {
$pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
preg_match_all($pa, $string, $t_string);
if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
return join('', array_slice($t_string[0], $start, $sublen));
}else{
$start = $start*2;
$sublen = $sublen*2;
$strlen = strlen($string);
$tmpstr = '';
for($i=0; $i< $strlen; $i++)
{
if($i>=$start && $i< ($start+$sublen)){
if(ord(substr($string, $i, 1))>129){
$tmpstr.= substr($string, $i, 2);
}else{
$tmpstr.= substr($string, $i, 1);
}
}
if(ord(substr($string, $i, 1))>129) $i++;
}
if(strlen($tmpstr)< $strlen ) $tmpstr.= "...";
return $tmpstr;
}
}
7、求两个已知经纬度之间的距离,单位为米
/**
*求两个已知经纬度之间的距离,单位为米
*@param lng1,lng2 经度
*@param lat1,lat2 纬度
*@return float 距离,单位米
**/
function getdistance($lng1,$lat1,$lng2,$lat2){
//将角度转为狐度
$radLat1=deg2rad($lat1);//deg2rad()函数将角度转换为弧度
$radLat2=deg2rad($lat2);
$radLng1=deg2rad($lng1);
$radLng2=deg2rad($lng2);
$a=$radLat1-$radLat2;
$b=$radLng1-$radLng2;
$s=2*asin(sqrt(pow(sin($a/2),2)+cos($radLat1)*cos($radLat2)*pow(sin($b/2),2)))*6378.137*1000;
return $s;
}
8、xml转数组
function xml_to_array($xml){
$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
if(preg_match_all($reg, $xml, $matches)){
$count = count($matches[0]);
for($i = 0; $i < $count; $i++){
$subxml= $matches[2][$i];
$key = $matches[1][$i];
if(preg_match( $reg, $subxml )){
$arr[$key] = $this->xml_to_array( $subxml );
}else{
$arr[$key] = $subxml;
}
}
}
return $arr;
}