PHP常用封裝函數

//php 獲取圖片後綴
//第1種方法:
function get_extension($file)
{
substr(strrchr($file, '.'), 1);
}
//第2種方法:
function get_extension($file)
{
return substr($file, strrpos($file, '.')+1);
}
//第3種方法:
function get_extension($file)
{
$arr = explode('.', $file);
return end($arr);
}
//第4種方法:
function get_extension($file)
{
$info = pathinfo($file);
return $info['extension'];
}
//第5種方法:
function get_extension($file)
{
return pathinfo($file, PATHINFO_EXTENSION);
}


/**
 * @Author: Ding Jianlong
 * @Date:  2019-03-07 16:14:04
 * @Last Modified by:  Ding Jianlong
 * @Last Modified time: 2019-03-20 13:45:12
 */
header('content-type:text/html;charset=utf-8');
//獲取頂級域名
function getTopHost($url){
 $url = strtolower($url);  //首先轉成小寫
 $hosts = parse_url($url);
 $host = $hosts['host'];
 //查看是幾級域名
  $data = explode('.', $host);
  $n = count($data);
  //判斷是否是雙後綴
  $preg = '/[\w].+\.(com|net|org|gov|edu)\.cn$/';
  if(($n > 2) && preg_match($preg,$host)){
   //雙後綴取後3位
   $host = $data[$n-3].'.'.$data[$n-2].'.'.$data[$n-1];
  }else{
   //非雙後綴取後兩位
   $host = $data[$n-2].'.'.$data[$n-1];
  }
  return $host;
}




/**
 * 能用的隨機數生成
 * @param string $type 類型 alpha/alnum/numeric/nozero/unique/md5/encrypt/sha1
 * @param int    $len  長度
 * @return string
 */
  function build($type = 'alnum', $len = 8)
{
    switch ($type) {
	   case 'alpha':
	   case 'alnum':
	   case 'numeric':
	   case 'nozero':
		  switch ($type) {
			 case 'alpha':
				$pool = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
			 case 'alnum':
				$pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
				break;
			 case 'numeric':
				$pool = '0123456789';
				break;
			 case 'nozero':
				$pool = '123456789';
				break;
		  }
		  return substr(str_shuffle(str_repeat($pool, ceil($len / strlen($pool)))), 0, $len);
	   case 'unique':
	   case 'md5':
		  return md5(uniqid(mt_rand()));
	   case 'encrypt':
	   case 'sha1':
		  return sha1(uniqid(mt_rand(), true));
    }
}


//純數字的四位隨機數
//rand(1000,9999)
//數字和字符混搭的四位隨機字符串:
function GetRandStr($len) 
{ 
    $chars = array( 
        "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",  
        "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",  
        "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",  
        "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",  
        "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",  
        "3", "4", "5", "6", "7", "8", "9" 
    ); 
    $charsLen = count($chars) - 1; 
    shuffle($chars);   
    $output = ""; 
    for ($i=0; $i<$len; $i++) 
    { 
        $output .= $chars[mt_rand(0, $charsLen)]; 
    }  
    return $output;  
} 
//echo GetRandStr(4);




/**
 * 生成不重複的隨機數
 * @param  int $start  需要生成的數字開始範圍
 * @param  int $end    結束範圍
 * @param  int $length 需要生成的隨機數個數
 * @return array       生成的隨機數
 */
function get_rand_number($start=1,$end=10,$length=4){
    $connt=0;
    $temp=array();
    while($connt<$length){
        $temp[]=mt_rand($start,$end);
        $data=array_unique($temp);
        $connt=count($data);
    }
    sort($data);
    return $data;
}

瞭解更多,請加QQ羣討論:292539913

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