php工作中常用的方法總結

php工作中常用的方法總結,會不定期更新哦,喜歡的朋友記得點贊收藏


PHP隨機生成指定時間範圍的時間

/**
 * 生成某個範圍內的隨機時間
 * Author:劉星麟
 * @param $beginTime 起始時間 格式爲 Y-m-d H:i:s
 * @param string $endTime 結束時間 格式爲 Y-m-d H:i:s
 * @param bool $now 是否是時間戳 格式爲 Boolean
 * @return false|int|string
 */
function random_date($beginTime, $endTime="", $now = true) {
    $begin = strtotime($beginTime);
    $end = $endTime == "" ? mktime() : strtotime($endTime);
    if ($begin === false || $end === false) {
        return false;
    }
    $timestamp = rand($begin, $end);
    return $now ? date("Y-m-d H:i:s", $timestamp) : $timestamp;
}

curl發送get、post、put、delete請求


if (!function_exists('curl_data')) {
    /**
     * curl 請求
     * Author:劉星麟
     * @param $url
     * @param $data
     * @param string $method 支持 GET  POST  PUT  DELETE
     * @param string $type
     * @return bool|string
     */
    function curl_data($url, $data, $method = 'POST', $type = 'form-data')
    {
        //初始化
        $ch = curl_init();
        header('Content-Type:application/json; charset=utf-8');
        $headers = [
            'form-data' => ['Content-Type: multipart/form-data'], 'json' => ['Content-Type: application/json'],
        ];
        
        //統一轉化爲大寫
        $method = strtoupper($method);
        if ($method == 'GET') {
            if ($data) {
                $querystring = http_build_query($data);
                $url = $url . '?' . $querystring;
            }
        }
        
        // 請求頭,可以傳數組
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers[$type]);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        if ($method == 'POST') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        if ($method == 'PUT') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        if ($method == 'DELETE') {
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);           //最大相應超時時間
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 不從證書中檢查SSL加密算法是否存在
        $output = curl_exec($ch);
        
        curl_close($ch);
        return $output;
    }
}

文件寫入

if (!function_exists('write_file')) {
    /**
     * 文件寫入
     * Author:劉星麟
     * @param $filePath 文件路徑
     * @param $fileName 文件名稱
     * @param $content  寫入內容
     * @return bool|int
     */
    function write_file($filePath, $fileName, $content)
    {
        //判斷文件是否存在,如果不存在則創建文件夾
        if (!is_dir($filePath)) {
            @mkdir($filePath, 0755, true);
        }
        $directory_separator = substr($filePath, (strlen($filePath) - 1), 1);
        if ($directory_separator != '/') {
            $filePath = $filePath . '/';
        }
        //文件名稱
        $fileName = $filePath . DIRECTORY_SEPARATOR . $fileName;
        return file_put_contents($fileName, $content);
    }
}

字符串截取 

if (!function_exists('utf_substr')) {
    /**
     * 字符串的截取
     * Author:劉星麟
     * @param $str string 原字符串
     * @param $start int 開始截取位置
     * @param $len int 截取的長度
     * @param $flag string 標誌符
     * @return string
     */
    function utf_substr($str, $start = 0, $len = 130, $flag = '')
    {
        if (str2len($str) < $len) {
            $flag = '';
        }
        return mb_substr(str_replace(["\r", "\n"], "", $str), $start, $len) . $flag;
    }
}


 字符串編碼轉換

if (!function_exists('charset')) {
    /**
     * 字符串轉換成UTF-8
     * Author:劉星麟
     * @param string $data
     * @return string
     */
    function charset($data = '')
    {
        if (!empty($data)) {
            $fileType = mb_detect_encoding($data, ['UTF-8', 'GBK', 'LATIN1', 'BIG5', 'GB2312']);
            if ($fileType != 'UTF-8') {
                $data = mb_convert_encoding($data, 'utf-8', $fileType);
            }
        }
        return $data;
    }
}

 判斷字符串長度

if (!function_exists('str2len')) {
    /**
     * 判斷字符長度
     * Author:劉星麟
     * @param string $str
     * @return float|int
     */
    function str2len($str = '')
    {
        $str2len = (strlen($str) + mb_strlen($str, "UTF-8")) / 2;
        return $str2len;
    }
}

判斷數據是否爲空

if (!function_exists('_empty')) {
    /**
     * 判斷是否爲空
     * Author:劉星麟
     * @param $param
     * @return bool
     */
    function _empty($param)
    {
        if (empty($param) && !is_numeric($param)) {
            return true;
        } else {
            return false;
        }
    }
}

檢測json數據格式是否正確

if (!function_exists('json_validate')) {
    /**
     * 檢測json數據格式是否正確
     * Author:劉星麟
     * @param $string
     * @return bool
     */
    function json_validate($string)
    {
        if (is_string($string)) {
            @json_decode($string);
            return (json_last_error() === JSON_ERROR_NONE);
        } else {
            return false;
        }
    }
}

獲取當前url請求協議

更多請參考: https://blog.csdn.net/liuxl57805678/article/details/100302414

if (!function_exists('get_request_scheme')) {
    /**
     * 獲取當前url請求協議
     * Author:劉星麟
     * @return string
     */
    function get_request_scheme()
    {
        return ((int)$_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://';
    }
}

PHP數組轉字符串

https://blog.csdn.net/liuxl57805678/article/details/103288924

PHP獲取用戶IP地址

https://blog.csdn.net/liuxl57805678/article/details/103297827

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章