常用函数收集

不定时更新 自己收集到 觉得有用的常见简单自定义函数 供自己速查

/**
 * 取上一步来源地址
 *
 * @param
 * @return string 字符串类型的返回结果
 */
function get_referer()
{
    return empty($_SERVER['HTTP_REFERER']) ? '' : $_SERVER['HTTP_REFERER'];
}

CURL 的封装

/**
 * CURL请求
 * @param $url 请求url地址
 * @param $method 请求方法 get post
 * @param null $postfields post数据数组  post的数据
 * @param array $headers 请求header信息  头信息参数
 * @param bool|false $debug 调试开启 默认false
 * @return mixed
 */
function http_request($url, $method = "GET", $postfields = null, $headers = array(), $debug = false)
{
    $method = strtoupper($method);
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 60); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */
    curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
    switch ($method) {
        case "POST":
            curl_setopt($ci, CURLOPT_POST, true);
            if (!empty($postfields)) {
                $tmpdatastr = is_array($postfields) ? http_build_query($postfields) : $postfields;
                curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr);
            }
            break;
        default:
            curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */
            break;
    }
    $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE;
    curl_setopt($ci, CURLOPT_URL, $url);
    if ($ssl) {
        curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在
    }
    //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/
    curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLINFO_HEADER_OUT, true);
    /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */
    $response = curl_exec($ci);
    $requestinfo = curl_getinfo($ci);
    $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
    if ($debug) {
        echo "=====post data======\r\n";
        var_dump($postfields);
        echo "=====info===== \r\n";
        print_r($requestinfo);
        echo "=====response=====\r\n";
        print_r($response);
    }
    curl_close($ci);
    return $response;
}

杂项1

function getImg($img, $type = '')
{
    $img = str_replace('\\', '/', $img);
    if (empty($type)) return $img;
    $imgs = explode('.', $img);
    $imgs[count($imgs) - 1] = $type . '.' . $imgs[count($imgs) - 1];
    return join('.', $imgs);
}

function getWeek($date)
{
    $t = strtotime($date);
    $weekarray = array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    return $weekarray[date('w', $t)];
}
function getDates($num = 30)
{
    $start = $_SERVER['REQUEST_TIME'];
    $end = $start + $num * 86400;
    $weekarray = array("周日", "周一", "周二", "周三", "周四", "周五", "周六");
    $today = date('Y-m-d', $start);
    $date = [];
    for ($i = $start; $i <= $end; $i = $i + 86400) {
        $day = date('Y-m-d', $i);
        $w = date('w', $i);

        if ($day == $today) {
            $day2 = '今天';
        } else {
            $day2 = date('m-d', $i);
        }
        $date[] = [
            'day' => $day,
            'w' => $weekarray[$w],
            'day2' => $day2
        ];
    }
    return $date;
}
function formatDateTime($dataTime)
{
    $t = $_SERVER['REQUEST_TIME'] - $dataTime;

    if ($t < 360) {
        return '刚刚';
    }
    if ($t < 720) {
        return '10分前';
    }
    if ($t < 3600) {
        return '1小时内';
    }
    if ($t < 86400) {
        return '今天';
    }
    if ($t < 86400 * 2) {
        return '昨天';
    }
    if ($t < 86400 * 3) {
        return '前天';
    }
    if ($t < 86400 * 7) {
        return '一周内';
    }
    if ($t < 86400 * 15) {
        return '半月内';
    }
    if ($t < 86400 * 30) {
        return '一月内';
    }
    if ($t < 86400 * 90) {
        return '三月内';
    }
    if ($t < 86400 * 180) {
        return '半年内';
    }
    if ($t < 86400 * 365) {
        return '一年内';
    }
    if ($t >= 86400 * 365) {
        return '一年以上';
    }
}
/*
 * 经度纬度 转换成距离
 * $lat1 $lng1 是 数据的经度纬度
 * $lat2,$lng2 是获取定位的经度纬度
 */

function rad($d)
{
    return $d * 3.1415926535898 / 180.0;
}

function getDistanceNone($lat1, $lng1, $lat2, $lng2)
{
    $EARTH_RADIUS = 6378.137;
    $radLat1 = rad($lat1);
    //echo $radLat1;
    $radLat2 = rad($lat2);
    $a = $radLat1 - $radLat2;
    $b = rad($lng1) - rad($lng2);
    $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
    $s = $s * $EARTH_RADIUS;
    $s = round($s * 10000);
    return $s;
}

function getDistance($lat1, $lng1, $lat2, $lng2)
{
    $s = getDistanceNone($lat1, $lng1, $lat2, $lng2);
    $s = $s / 10000;
    if ($s < 1) {
        $s = round($s * 1000);
        $s .= 'm';
    } else {
        $s = round($s, 2);
        $s .= 'km';
    }
    return $s;
}
/**
 * 判断输入的字符串是否是一个合法的电话号码(仅限中国大陆)
 *
 * @param string $string
 * @return boolean
 */
function isPhone($string)
{
    if (preg_match('/^[0,4]\d{2,3}-\d{7,8}$/', $string))
        return true;
    return false;
}

/**
 * 判断输入的字符串是否是一个合法的手机号(仅限中国大陆)
 *
 * @param string $string
 * @return boolean
 */
function isMobile($string)
{
    return ctype_digit($string) && (11 == strlen($string)) && ($string[0] == 1);
}
function SecurityEditorHtml($str)
{
    $farr = array(
        "/\s+/", //过滤多余的空白
        "/<(\/?)(script|i?frame|style|html|body|title|link|meta|\?|\%)([^>]*?)>/isU",
        "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU"
    );
    $tarr = array(
        " ",
        "<\\1\\2\\3>",
        "\\1\\2",
    );
    $str = preg_replace($farr, $tarr, $str);
    return $str;
}
//二维数组转换函数

//参数  1 数组 2 要排序的值 3 排序方式

function array_sort($arr, $keys, $type = 'asc')
{
    $keysvalue = $new_array = array();
    foreach ($arr as $k => $v) {
        $keysvalue[$k] = $v[$keys];
    }
    if ($type == 'asc') {
        asort($keysvalue);
    } else {
        arsort($keysvalue);
    }
    reset($keysvalue);
    foreach ($keysvalue as $k => $v) {
        $new_array[] = $arr[$k];
    }
    return $new_array;
}

记录你我
请先登录后发表评论
  • latest comments
  • 总共0条评论