Home >Backend Development >PHP Tutorial >Code examples for commonly used functions in php

Code examples for commonly used functions in php

黄舟
黄舟Original
2017-08-13 09:17:46996browse

Go directly to the code, I believe it will be used in the future

<?php

/**
 * @param $arr
 * @param $key_name
 * @return array
 * 将数据库中查出的列表以指定的 id 作为数组的键名 
 */
function convert_arr_key($arr, $key_name)
{
    $arr2 = array();
    foreach($arr as $key => $val){
        $arr2[$val[$key_name]] = $val;        
    }
    return $arr2;
}

function encrypt($str){
    return md5(C("AUTH_CODE").$str);
}
            
/**
 * 获取数组中的某一列
 * @param type $arr 数组
 * @param type $key_name  列名
 * @return type  返回那一列的数组
 */
function get_arr_column($arr, $key_name)
{
    $arr2 = array();
    foreach($arr as $key => $val){
        $arr2[] = $val[$key_name];        
    }
    return $arr2;
}


/**
 * 获取url 中的各个参数  类似于 pay_code=alipay&bank_code=ICBC-DEBIT
 * @param type $str
 * @return type
 */
function parse_url_param($str){
    $data = array();
    $parameter = explode(&#39;&&#39;,end(explode(&#39;?&#39;,$str)));
    foreach($parameter as $val){
        $tmp = explode(&#39;=&#39;,$val);
        $data[$tmp[0]] = $tmp[1];
    }
    return $data;
}


/**
 * 二维数组排序
 * @param $arr
 * @param $keys
 * @param string $type
 * @return array
 */
function array_sort($arr, $keys, $type = &#39;desc&#39;)
{
    $key_value = $new_array = array();
    foreach ($arr as $k => $v) {
        $key_value[$k] = $v[$keys];
    }
    if ($type == &#39;asc&#39;) {
        asort($key_value);
    } else {
        arsort($key_value);
    }
    reset($key_value);
    foreach ($key_value as $k => $v) {
        $new_array[$k] = $arr[$k];
    }
    return $new_array;
}


/**
 * 多维数组转化为一维数组
 * @param 多维数组
 * @return array 一维数组
 */
function array_multi2single($array)
{
    static $result_array = array();
    foreach ($array as $value) {
        if (is_array($value)) {
            array_multi2single($value);
        } else
            $result_array [] = $value;
    }
    return $result_array;
}

/**
 * 友好时间显示
 * @param $time
 * @return bool|string
 */
function friend_date($time)
{
    if (!$time)
        return false;
    $fdate = &#39;&#39;;
    $d = time() - intval($time);
    $ld = $time - mktime(0, 0, 0, 0, 0, date(&#39;Y&#39;)); //得出年
    $md = $time - mktime(0, 0, 0, date(&#39;m&#39;), 0, date(&#39;Y&#39;)); //得出月
    $byd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 2, date(&#39;Y&#39;)); //前天
    $yd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 1, date(&#39;Y&#39;)); //昨天
    $dd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;)); //今天
    $td = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 1, date(&#39;Y&#39;)); //明天
    $atd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 2, date(&#39;Y&#39;)); //后天
    if ($d == 0) {
        $fdate = &#39;刚刚&#39;;
    } else {
        switch ($d) {
            case $d < $atd:
                $fdate = date(&#39;Y年m月d日&#39;, $time);
                break;
            case $d < $td:
                $fdate = &#39;后天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < 0:
                $fdate = &#39;明天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < 60:
                $fdate = $d . &#39;秒前&#39;;
                break;
            case $d < 3600:
                $fdate = floor($d / 60) . &#39;分钟前&#39;;
                break;
            case $d < $dd:
                $fdate = floor($d / 3600) . &#39;小时前&#39;;
                break;
            case $d < $yd:
                $fdate = &#39;昨天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < $byd:
                $fdate = &#39;前天&#39; . date(&#39;H:i&#39;, $time);
                break;
            case $d < $md:
                $fdate = date(&#39;m月d日 H:i&#39;, $time);
                break;
            case $d < $ld:
                $fdate = date(&#39;m月d日&#39;, $time);
                break;
            default:
                $fdate = date(&#39;Y年m月d日&#39;, $time);
                break;
        }
    }
    return $fdate;
}


/**
 * 返回状态和信息
 * @param $status
 * @param $info
 * @return array
 */
function arrayRes($status, $info, $url = "")
{
    return array("status" => $status, "info" => $info, "url" => $url);
}
       
/**
 * @param $arr
 * @param $key_name
  * @param $key_name2
 * @return array
 * 将数据库中查出的列表以指定的 id 作为数组的键名 数组指定列为元素 的一个数组
 */
function get_id_val($arr, $key_name,$key_name2)
{
    $arr2 = array();
    foreach($arr as $key => $val){
        $arr2[$val[$key_name]] = $val[$key_name2];
    }
    return $arr2;
}

/**
 *  自定义函数 判断 用户选择 从下面的列表中选择 可选值列表:不能为空
 * @param type $attr_values
 * @return boolean
 */
function checkAttrValues($attr_values)
{        
    if((trim($attr_values) == &#39;&#39;) && ($_POST[&#39;attr_input_type&#39;] == &#39;1&#39;))        
        return false;
    else
        return true;
 }
 
 // 定义一个函数getIP() 客户端IP,
function getIP(){            
    if (getenv("HTTP_CLIENT_IP"))
         $ip = getenv("HTTP_CLIENT_IP");
    else if(getenv("HTTP_X_FORWARDED_FOR"))
            $ip = getenv("HTTP_X_FORWARDED_FOR");
    else if(getenv("REMOTE_ADDR"))
         $ip = getenv("REMOTE_ADDR");
    else $ip = "Unknow";
    return $ip;
}
// 服务器端IP
 function serverIP(){   
  return gethostbyname($_SERVER["SERVER_NAME"]);   
 }  
 
 
 /**
  * 自定义函数递归的复制带有多级子目录的目录
  * 递归复制文件夹
  * @param type $src 原目录
  * @param type $dst 复制到的目录
  */                        
//参数说明:            
//自定义函数递归的复制带有多级子目录的目录
function recurse_copy($src, $dst)
{
    $now = time();
    $dir = opendir($src);
    @mkdir($dst);
    while (false !== $file = readdir($dir)) {
        if (($file != &#39;.&#39;) && ($file != &#39;..&#39;)) {
            if (is_dir($src . &#39;/&#39; . $file)) {
                recurse_copy($src . &#39;/&#39; . $file, $dst . &#39;/&#39; . $file);
            }
            else {
                if (file_exists($dst . DIRECTORY_SEPARATOR . $file)) {
                    if (!is_writeable($dst . DIRECTORY_SEPARATOR . $file)) {
                        exit($dst . DIRECTORY_SEPARATOR . $file . &#39;不可写&#39;);
                    }
                    @unlink($dst . DIRECTORY_SEPARATOR . $file);
                }
                if (file_exists($dst . DIRECTORY_SEPARATOR . $file)) {
                    @unlink($dst . DIRECTORY_SEPARATOR . $file);
                }
                $copyrt = copy($src . DIRECTORY_SEPARATOR . $file, $dst . DIRECTORY_SEPARATOR . $file);
                if (!$copyrt) {
                    echo &#39;copy &#39; . $dst . DIRECTORY_SEPARATOR . $file . &#39; failed<br>&#39;;
                }
            }
        }
    }
    closedir($dir);
}

// 递归删除文件夹
function delFile($dir,$file_type=&#39;&#39;) {
    if(is_dir($dir)){
        $files = scandir($dir);
        //打开目录 //列出目录中的所有文件并去掉 . 和 ..
        foreach($files as $filename){
            if($filename!=&#39;.&#39; && $filename!=&#39;..&#39;){
                if(!is_dir($dir.&#39;/&#39;.$filename)){
                    if(empty($file_type)){
                        unlink($dir.&#39;/&#39;.$filename);
                    }else{
                        if(is_array($file_type)){
                            //正则匹配指定文件
                            if(preg_match($file_type[0],$filename)){
                                unlink($dir.&#39;/&#39;.$filename);
                            }
                        }else{
                            //指定包含某些字符串的文件
                            if(false!=stristr($filename,$file_type)){
                                unlink($dir.&#39;/&#39;.$filename);
                            }
                        }
                    }
                }else{
                    delFile($dir.&#39;/&#39;.$filename);
                    rmdir($dir.&#39;/&#39;.$filename);
                }
            }
        }
    }else{
        if(file_exists($dir)) unlink($dir);
    }
}

 
/**
 * 多个数组的笛卡尔积
*
* @param unknown_type $data
*/
function combineDika() {
    $data = func_get_args();
    $data = current($data);
    $cnt = count($data);
    $result = array();
    $arr1 = array_shift($data);
    foreach($arr1 as $key=>$item) 
    {
        $result[] = array($item);
    }        

    foreach($data as $key=>$item) 
    {                                
        $result = combineArray($result,$item);
    }
    return $result;
}


/**
 * 两个数组的笛卡尔积
 * @param unknown_type $arr1
 * @param unknown_type $arr2
*/
function combineArray($arr1,$arr2) {         
    $result = array();
    foreach ($arr1 as $item1) 
    {
        foreach ($arr2 as $item2) 
        {
            $temp = $item1;
            $temp[] = $item2;
            $result[] = $temp;
        }
    }
    return $result;
}
/**
 * 将二维数组以元素的某个值作为键 并归类数组
 * array( array(&#39;name&#39;=>&#39;aa&#39;,&#39;type&#39;=>&#39;pay&#39;), array(&#39;name&#39;=>&#39;cc&#39;,&#39;type&#39;=>&#39;pay&#39;) )
 * array(&#39;pay&#39;=>array( array(&#39;name&#39;=>&#39;aa&#39;,&#39;type&#39;=>&#39;pay&#39;) , array(&#39;name&#39;=>&#39;cc&#39;,&#39;type&#39;=>&#39;pay&#39;) ))
 * @param $arr 数组
 * @param $key 分组值的key
 * @return array
 */
function group_same_key($arr,$key){
    $new_arr = array();
    foreach($arr as $k=>$v ){
        $new_arr[$v[$key]][] = $v;
    }
    return $new_arr;
}

/**
 * 获取随机字符串
 * @param int $randLength  长度
 * @param int $addtime  是否加入当前时间戳
 * @param int $includenumber   是否包含数字
 * @return string
 */
function get_rand_str($randLength=6,$addtime=1,$includenumber=0){
    if ($includenumber){
        $chars=&#39;abcdefghijklmnopqrstuvwxyzABCDEFGHJKLMNPQEST123456789&#39;;
    }else {
        $chars=&#39;abcdefghijklmnopqrstuvwxyz&#39;;
    }
    $len=strlen($chars);
    $randStr=&#39;&#39;;
    for ($i=0;$i<$randLength;$i++){
        $randStr.=$chars[rand(0,$len-1)];
    }
    $tokenvalue=$randStr;
    if ($addtime){
        $tokenvalue=$randStr.time();
    }
    return $tokenvalue;
}

/**
 * CURL请求
 * @param $url 请求url地址
 * @param $method 请求方法 get post
 * @param null $postfields post数据数组
 * @param array $headers 请求header信息
 * @param bool|false $debug  调试开启 默认false
 * @return mixed
 */
function httpRequest($url, $method, $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(&#39;/^https:\/\//i&#39;,$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;
    //return array($http_code, $response,$requestinfo);
}

/**
 * 过滤数组元素前后空格 (支持多维数组)
 * @param $array 要过滤的数组
 * @return array|string
 */
function trim_array_element($array){
    if(!is_array($array))
        return trim($array);
    return array_map(&#39;trim_array_element&#39;,$array);
}

/**
 * 检查手机号码格式
 * @param $mobile 手机号码
 */
function check_mobile($mobile){
    if(preg_match(&#39;/1[34578]\d{9}$/&#39;,$mobile))
        return true;
    return false;
}

/**
 * 检查邮箱地址格式
 * @param $email 邮箱地址
 */
function check_email($email){
    if(filter_var($email,FILTER_VALIDATE_EMAIL))
        return true;
    return false;
}


/**
 *   实现中文字串截取无乱码的方法
 */
function getSubstr($string, $start, $length) {
      if(mb_strlen($string,&#39;utf-8&#39;)>$length){
          $str = mb_substr($string, $start, $length,&#39;utf-8&#39;);
          return $str.&#39;...&#39;;
      }else{
          return $string;
      }
}


/**
 * 判断当前访问的用户是  PC端  还是 手机端  返回true 为手机端  false 为PC 端
 * @return boolean
 */
/**
  * 是否移动端访问访问
  *
  * @return bool
  */
function isMobile()
{
        // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
    if (isset ($_SERVER[&#39;HTTP_X_WAP_PROFILE&#39;]))
    return true;

    // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
    if (isset ($_SERVER[&#39;HTTP_VIA&#39;]))
    {
    // 找不到为flase,否则为true
    return stristr($_SERVER[&#39;HTTP_VIA&#39;], "wap") ? true : false;
    }
    // 脑残法,判断手机发送的客户端标志,兼容性有待提高
    if (isset ($_SERVER[&#39;HTTP_USER_AGENT&#39;]))
    {
        $clientkeywords = array (&#39;nokia&#39;,&#39;sony&#39;,&#39;ericsson&#39;,&#39;mot&#39;,&#39;samsung&#39;,&#39;htc&#39;,&#39;sgh&#39;,&#39;lg&#39;,&#39;sharp&#39;,&#39;sie-&#39;,&#39;philips&#39;,&#39;panasonic&#39;,&#39;alcatel&#39;,&#39;lenovo&#39;,&#39;iphone&#39;,&#39;ipod&#39;,&#39;blackberry&#39;,&#39;meizu&#39;,&#39;android&#39;,&#39;netfront&#39;,&#39;symbian&#39;,&#39;ucweb&#39;,&#39;windowsce&#39;,&#39;palm&#39;,&#39;operamini&#39;,&#39;operamobi&#39;,&#39;openwave&#39;,&#39;nexusone&#39;,&#39;cldc&#39;,&#39;midp&#39;,&#39;wap&#39;,&#39;mobile&#39;);
        // 从HTTP_USER_AGENT中查找手机浏览器的关键字
        if (preg_match("/(" . implode(&#39;|&#39;, $clientkeywords) . ")/i", strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;])))
            return true;
    }
        // 协议法,因为有可能不准确,放到最后判断
    if (isset ($_SERVER[&#39;HTTP_ACCEPT&#39;]))
    {
    // 如果只支持wml并且不支持html那一定是移动设备
    // 如果支持wml和html但是wml在html之前则是移动设备
        if ((strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) !== false) && (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;) === false || (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) < strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;))))
        {
            return true;
        }
    }
            return false;
 } 

//php获取中文字符拼音首字母
function getFirstCharter($str){
      if(empty($str))
      {
            return &#39;&#39;;          
      }
      $fchar=ord($str{0});
      if($fchar>=ord(&#39;A&#39;)&&$fchar<=ord(&#39;z&#39;)) return strtoupper($str{0});
      $s1=iconv(&#39;UTF-8&#39;,&#39;gb2312&#39;,$str);
      $s2=iconv(&#39;gb2312&#39;,&#39;UTF-8&#39;,$s1);
      $s=$s2==$str?$s1:$str;
      $asc=ord($s{0})*256+ord($s{1})-65536;
     if($asc>=-20319&&$asc<=-20284) return &#39;A&#39;;
     if($asc>=-20283&&$asc<=-19776) return &#39;B&#39;;
     if($asc>=-19775&&$asc<=-19219) return &#39;C&#39;;
     if($asc>=-19218&&$asc<=-18711) return &#39;D&#39;;
     if($asc>=-18710&&$asc<=-18527) return &#39;E&#39;;
     if($asc>=-18526&&$asc<=-18240) return &#39;F&#39;;
     if($asc>=-18239&&$asc<=-17923) return &#39;G&#39;;
     if($asc>=-17922&&$asc<=-17418) return &#39;H&#39;;
     if($asc>=-17417&&$asc<=-16475) return &#39;J&#39;;
     if($asc>=-16474&&$asc<=-16213) return &#39;K&#39;;
     if($asc>=-16212&&$asc<=-15641) return &#39;L&#39;;
     if($asc>=-15640&&$asc<=-15166) return &#39;M&#39;;
     if($asc>=-15165&&$asc<=-14923) return &#39;N&#39;;
     if($asc>=-14922&&$asc<=-14915) return &#39;O&#39;;
     if($asc>=-14914&&$asc<=-14631) return &#39;P&#39;;
     if($asc>=-14630&&$asc<=-14150) return &#39;Q&#39;;
     if($asc>=-14149&&$asc<=-14091) return &#39;R&#39;;
     if($asc>=-14090&&$asc<=-13319) return &#39;S&#39;;
     if($asc>=-13318&&$asc<=-12839) return &#39;T&#39;;
     if($asc>=-12838&&$asc<=-12557) return &#39;W&#39;;
     if($asc>=-12556&&$asc<=-11848) return &#39;X&#39;;
     if($asc>=-11847&&$asc<=-11056) return &#39;Y&#39;;
     if($asc>=-11055&&$asc<=-10247) return &#39;Z&#39;;
     return null;
}

The above is the detailed content of Code examples for commonly used functions in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn