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

你可能感兴趣的:(php)