php常用的公共方法

本人写项目常用的公共方法

不定期更新,有什么补充欢迎私聊

<?php

//后台设置返回

function returnajax($code = 0, $msg = '', $data = [], $count = 0)

{

  $arr = [

    'code' => $code,

    'msg' => $msg,

    'data' => $data,

    'count' => $count

  ];

  return json_encode($arr);

}

//获取ip

function get_client_ip()

{

  $ip = $_SERVER['REMOTE_ADDR'];

  if (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {

    $ip = $_SERVER['HTTP_CLIENT_IP'];

  } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {

    foreach ($matches[0] as $xip) {

      if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {

        $ip = $xip;

        break;

      }

    }

  }

  return $ip;

}

//获取客户端信息

function getuseragent()

{

  return $_SERVER['HTTP_USER_AGENT'];

}

/*

   $url 请求的接口地址

*/

function sendhttp_get($url)

{

  $curl = curl_init();

  curl_setopt($curl, CURLOPT_URL, $url);

  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

  curl_setopt($curl, CURLOPT_POST, 1);

  curl_setopt($curl, CURLOPT_POSTFIELDS, array());

  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  $result = json_decode(curl_exec($curl), true);

  curl_close($curl);

  return $result;

}

//   $data 上传资源的地址

function sendhttps_post($url, $data)

{

  $curl = curl_init();

  curl_setopt($curl, CURLOPT_URL, $url);

  curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

  curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

  curl_setopt($curl, CURLOPT_POST, 1);

  curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

  $result = curl_exec($curl);

  if (curl_errno($curl)) {

    return 'Errno' . curl_error($curl);

  }

  curl_close($curl);

  return $result;

}

// 验证手机号码

function check_phone($value)

{

  if (!$value) {

    return false;

  } elseif (!is_numeric($value)) {

    return false;

  } elseif (strlen($value) != 11) {

    return false;

  }

  return true;

}

// 验证邮件地址

function check_email($value)

{

  if (!$value) {

    return false;

  } elseif (!preg_match('/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/', $value)) {

    return false;

  } elseif (strpos($value, '"') !== false || strpos($value, '\'') !== false) {

    return false;

  }

  return true;

}

//计算经纬距离

function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit = 2, $decimal = 2)

{

  $EARTH_RADIUS = 6370.996; // 地球半径系数

  $PI = 3.1415926;

  $radLat1 = $latitude1 * $PI / 180.0;

  $radLat2 = $latitude2 * $PI / 180.0;

  $radLng1 = $longitude1 * $PI / 180.0;

  $radLng2 = $longitude2 * $PI / 180.0;

  $a = $radLat1 - $radLat2;

  $b = $radLng1 - $radLng2;

  $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) *         pow(sin($b / 2), 2)));

  $distance = $distance * $EARTH_RADIUS * 1000;

  if ($unit == 2) {

    $distance = $distance / 1000;

  }

  return round($distance, $decimal); //四舍五入

}

//根据生日计算年龄

function birthday($birthday)

{

  $age = strtotime($birthday);

  if ($age === false) {

    return false;

  }

  list($y1, $m1, $d1) = explode("-", date("Y-m-d", $age));

  $now = strtotime("now");

  list($y2, $m2, $d2) = explode("-", date("Y-m-d", $now));

  $age = $y2 - $y1;

  if ((int)($m2 . $d2) < (int)($m1 . $d1))

    $age -= 1;

  return $age;

}

//计算几分钟,几小时,几天前

function time_tran($the_time)

{

  $now_time = date("Y-m-d H:i:s", time());

  //echo $now_time;

  $now_time = strtotime($now_time);

  $show_time = strtotime($the_time);

  $dur = $now_time - $show_time;

  if ($dur < 0) {

    return $the_time;

  } else {

    if ($dur < 60) {

      return $dur . '秒前';

    } else {

      if ($dur < 3600) {

        return floor($dur / 60) . '分钟前';

      } else {

        if ($dur < 86400) {

          return floor($dur / 3600) . '小时前';

        } else {

          if ($dur < 259200) { //3天内

            return floor($dur / 86400) . '天前';

          } else {

            return $the_time;

          }

        }

      }

    }

  }

}


 

function getRandomString($len, $chars = null)

{

  if (is_null($chars)) {

    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

  }

  for ($i = 0, $str = '', $lc = strlen($chars) - 1; $i < $len; $i++) {

    $str .= $chars[mt_rand(0, $lc)];

  }

  return $str;

}

猜你喜欢

转载自blog.csdn.net/zongxingfengyun/article/details/120198642