非常有用的PHP函数

收集整理的非常有用的PHP函数

//type 0:加密 1:解密
function encryption($value,$type=0){
// $key=md5(‘www.tongpankt.com’);
$key=config(encryption_key);
if($type == 0){//加密
return str_replace(=, , base64_encode($value ^ $key));
}else{
$value=base64_decode($value);
return $value ^ $key;
}
}
/**
 * 根据二维数组某个key分组
 * $arr : 二维数组
 * $key: 需要分组的key
 */
function array_group_by($arr, $key)
{
    $grouped = [];
    foreach ($arr as $value) {
        $grouped[$value[$key]][] = $value;
    }
    // Recursively build a nested grouping if more parameters are supplied
    // Each grouped array value is grouped according to the next sequential key
    if (func_num_args() > 2) {
        $args = func_get_args();
        foreach ($grouped as $key => $value) {
            $parms = array_merge([$value], array_slice($args, 2, func_num_args()));
            $grouped[$key] = call_user_func_array('array_group_by', $parms);
        }
    }
    return $grouped;
}
/**
 * $arr 二维数组
 * 根据数组中的某个键合并数组
 * array (
  0 => 
  array (
    'id' => 1023,
    'work_date' => '2018-07-01 00:00:00',
  ),
  1 => 
  array (
    'id' => 1245,
    'work_date' => '2018-07-02 00:00:00',
  ),
  2 => 
  array (
    'id' => 1547,
    'work_date' => '2018-07-03 00:00:00',
  ),
 */
function arrKey($arr)
{
    $res = array(); //想要的结果
    foreach ($result as $k => $v) {
      $res[$v['time']][] = $v;
    }
}
/**
 *  获取 月 起始时间戳和结束时间戳
 */
function theMonth($y,$m='')
{
    if ($y == '' || $y == null) {
        throw new Exception("参数年份$y未传递!");
    }

    if ($m == '') {
        $bg = mktime(0,0,0,date('m'),1,date('Y'));
        $ed = mktime(23,59,59,date('m'),date('t'),date('Y'));    
    }else{
        $month = $y."-".$m;//当前年月  
        $bg = strtotime($month);//指定月份月初时间戳  
        $ed = mktime(23, 59, 59, date('m', strtotime($month))+1, 00);//指定月份月末时间戳  
    }

    return  [$bg,$ed];
}
/*
* 判断 $array中是否存在$value;

function deep_in_array($value,$array){
	foreach($array as $item){
		if(!is_array($item)){
			if ($item == $value){
				return true;
			}else{
				continue;
			}
		}
		if(in_array($value, $item)){
			return true;
		}else if(deep_in_array($value, $item)){
			return true;
		}
	}
	return false;
}*/
//生成随机字符串
function generate_password( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $password = '';
    for ( $i = 0; $i < $length; $i++ )
    {
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }
    return $password;
}

/*
* array unique_rand( int $min, int $max, int $num )
* 生成一定数量的不重复随机数
* $min 和 $max: 指定随机数的范围
* $num: 指定生成数量
*/

function unique_rand($min=100000, $max=999999, $num=6000) {
   $count = 0;
   $return = array();
   while ($count < $num) {
       $return[] = mt_rand($min, $max);
       $return = array_flip(array_flip($return));
       $count = count($return);
   }
   shuffle($return);
   return $return;
}
//获取浏览器和设备
function getAgentInfo(){
    $arr = array();
    $agent = $_SERVER['HTTP_USER_AGENT'];
    $browser = array('MSIE','Firefox','QQBrowser','QQ/','UCBrowser','MicroMessenger','Edge','Chrome','Opera','OPR','Safari','Trident/');
    $system = array('Windows Phone','Windows','Android','iPhone','iPad');
    foreach($browser as $v){
        if(stripos($agent,$v) !== false){
            $arr['browser'] = $v;
            break;
        }
    }
    foreach($system as $v){
        if(stripos($agent,$v)){
            $arr['system'] = $v;
            break;
        }
    }
    return $arr;
}
 
//判断微信
function is_weixin(){
    if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
        return true;
    }
    return false;
}

加密解密

function encryptDecrypt($key, $string, $decrypt)
{
    if($decrypt){
        $decrypted = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($string), MCRYPT_MODE_CBC, md5(md5($key))), "12");
        return $decrypted;
    }else{
        $encrypted = base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, md5($key), $string, MCRYPT_MODE_CBC, md5(md5($key))));
        return $encrypted;
    }
}

生成随机字符串

function generateRandomString($length = 10)
{
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

获取文件扩展名

function getExtension($filename)
{
  $myext = substr($filename, strrpos($filename, '.'));
  return str_replace('.','',$myext);
}

获取文件大小并格式化

function formatSize($size)
{
    $sizes = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB");
    if ($size == 0) {
		return('n/a');
	} else {
      return (round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $sizes[$i]);
	}
}

PHP替换标签字符

function stringParser($string,$replacer)
{
    $result = str_replace(array_keys($replacer), array_values($replacer),$string);
    return $result;
}

列出目录下的文件

function listDirFiles($DirPath)
{
    if($dir = opendir($DirPath)){
         while(($file = readdir($dir))!== false){
                if(!is_dir($DirPath.$file))
                {
                    echo "filename: $file<br />";
                }
         }
    }
}

获取当前页面的url

function curPageURL()
{
	$pageURL = 'http';
	if (!empty($_SERVER['HTTPS'])) {$pageURL .= "s";}
	$pageURL .= "://";
	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
	return $pageURL;
}

强制下载

function download($filename)
{
    if ((isset($filename))&&(file_exists($filename))){
       header("Content-length: ".filesize($filename));
       header('Content-Type: application/octet-stream');
       header('Content-Disposition: attachment; filename="' . $filename . '"');
       readfile("$filename");
    } else {
       echo "Looks like file does not exist!";
    }
}

截取

/*
 Utf-8、gb2312都支持的汉字截取函数
 cut_str(字符串, 截取长度, 开始长度, 编码);
 编码默认为 utf-8
 开始长度默认为 0
*/
function cutStr($string, $sublen, $start = 0, $code = 'UTF-8')
{
    if($code == 'UTF-8'){
        $pa = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/";
        preg_match_all($pa, $string, $t_string);

        if(count($t_string[0]) - $start > $sublen) return join('', array_slice($t_string[0], $start, $sublen))."...";
        return join('', array_slice($t_string[0], $start, $sublen));
    }else{
        $start = $start*2;
        $sublen = $sublen*2;
        $strlen = strlen($string);
        $tmpstr = '';

        for($i=0; $i<$strlen; $i++){
            if($i>=$start && $i<($start+$sublen)){
                if(ord(substr($string, $i, 1))>129){
                    $tmpstr.= substr($string, $i, 2);
                }else{
                    $tmpstr.= substr($string, $i, 1);
                }
            }
            if(ord(substr($string, $i, 1))>129) $i++;
        }
        if(strlen($tmpstr)<$strlen ) $tmpstr.= "...";
        return $tmpstr;
    }
}

获取用户真实IP

function getIp()
{
	if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		$ip = getenv("HTTP_CLIENT_IP");
	else
		if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
			$ip = getenv("HTTP_X_FORWARDED_FOR");
		else
			if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
				$ip = getenv("REMOTE_ADDR");
			else
				if (isset ($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
					$ip = $_SERVER['REMOTE_ADDR'];
				else
					$ip = "unknown";
	return ($ip);
}

防止注入

function injCheck($sql_str)
{
	$check = preg_match('/select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/', $sql_str);
	if ($check) {
		echo '非法字符!!'.$sql_str;
		exit;
	} else {
		return $sql_str;
	}
}

页面提示跳转

function message($msgTitle,$message,$jumpUrl)
{
	$str = '<!DOCTYPE HTML>';
	$str .= '<html>';
	$str .= '<head>';
	$str .= '<meta charset="utf-8">';
	$str .= '<title>页面提示</title>';
	$str .= '<style type="text/css">';
	$str .= '*{margin:0; padding:0}a{color:#369; text-decoration:none;}a:hover{text-decoration:underline}body{height:100%; font:12px/18px Tahoma, Arial,  sans-serif; color:#424242; background:#fff}.message{width:450px; height:120px; margin:16% auto; border:1px solid #99b1c4; background:#ecf7fb}.message h3{height:28px; line-height:28px; background:#2c91c6; text-align:center; color:#fff; font-size:14px}.msg_txt{padding:10px; margin-top:8px}.msg_txt h4{line-height:26px; font-size:14px}.msg_txt h4.red{color:#f30}.msg_txt p{line-height:22px}';
	$str .= '</style>';
	$str .= '</head>';
	$str .= '<body>';
	$str .= '<div class="message">';
	$str .= '<h3>'.$msgTitle.'</h3>';
	$str .= '<div class="msg_txt">';
	$str .= '<h4 class="red">'.$message.'</h4>';
	$str .= '<p>系统将在 <span style="color:blue;font-weight:bold">3</span> 秒后自动跳转,如果不想等待,直接点击 <a href="{$jumpUrl}">这里</a> 跳转</p>';
	$str .= "<script>setTimeout('location.replace(\'".$jumpUrl."\')',2000)</script>";
	$str .= '</div>';
	$str .= '</div>';
	$str .= '</body>';
	$str .= '</html>';
	echo $str;
}

时间长度转换

function changeTimeType($seconds)
{
	if ($seconds > 3600) {
		$hours = intval($seconds / 3600);
		$minutes = $seconds % 3600;
		$time = $hours . ":" . gmstrftime('%M:%S', $minutes);
	} else {
		$time = gmstrftime('%H:%M:%S', $seconds);
	}
	return $time;
}

转:https://www.helloweba.com/view-blog-281.html#pageurl




导航