PHP函数大全--对象转数组

发布于:2020-07-09 17:16:02


/**
 * 用户名、邮箱、手机号掩饰
 * @param unknown $str
 */
function hideStr($str)
{
    if (strpos($str, '@')) {
        $email_array = explode("@", $str);
        $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3);
        $count = 0;
        $str = preg_replace('/([\d\w+_-]{0,100})@/', '*****@', $str, -1, $count);
        $res = $prevfix . $str;
    } else {
        $pattern = '/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/';
        if (preg_match($pattern, $str)) {
            $res = substr_replace($str, '****', 3, 4);
        } else {
            if (preg_match("/[\x{4e00}-\x{9fa5}]+/u", $str)) {
                $len = mb_strlen($str, 'UTF-8');
                if ($len >= 3) {
                    $res = mb_substr($str, 0, 1, 'UTF-8') . '******' . mb_substr($str, -1, 1, 'UTF-8');
                } elseif ($len == 2) {
                    $res = mb_substr($str, 0, 1, 'UTF-8') . '******';
                }
            } else {
                $len = strlen($str);
                if ($len >= 3) {
                    $res = substr($str, 0, 1) . '******' . substr($str, -1);
                } elseif ($len == 2) {
                    $res = substr($str, 0, 1) . '******';
                }
            }
        }
    }
    return $res;
}



/**
 * 对象 转 数组 *
 * @param object $obj 对象
 * @return array
 */
function object_to_array($obj)
{
    $obj = (array)$obj;
    foreach ($obj as $k => $v) {
        if (gettype($v) == 'resource') {
            return;
        }
        if (gettype($v) == 'object' || gettype($v) == 'array') {
            $obj[$k] = (array)object_to_array($v);
        }
    }
    return $obj;
}


/**返回大小*/

function return_size($bytes, $separator='')
{
	//utility functions
	$kb = 1024;          //Kilobyte   
	$mb = 1024 * $kb;    //Megabyte   
	$gb = 1024 * $mb;    //Gigabyte   
	$tb = 1024 * $gb;    //Terabyte   

	if($bytes < $kb)   
		return $bytes."{$separator}B";   
	else if($bytes < $mb)   
		return round($bytes/$kb,2)."{$separator}KB";   
	else if($bytes < $gb)   
		return round($bytes/$mb,2)."{$separator}MB";   
	else if($bytes < $tb)   
		return round($bytes/$gb,2)."{$separator}GB";   
	else  
		return round($bytes/$tb,2)."{$separator}TB";   
}




/**返回目录大小*/

function getDirSize($dir)
{
    $sizeResult = 0;
    $handle = opendir($dir);
    while (false !== ($FolderOrFile = readdir($handle)))
    { 
        if($FolderOrFile != "." && $FolderOrFile != "..") { 
            if (is_dir("$dir/$FolderOrFile")) { 
                $sizeResult += getDirSize("$dir/$FolderOrFile"); 
            } else { 
                $sizeResult += filesize("$dir/$FolderOrFile"); 
            }
        } 
    }
    closedir($handle);
    return $sizeResult;
}

/**删除目录*/

function removeDir($dir)
{
    if (strpos($dir,'app') !== false) {
        return false ;
    }
    $handle = opendir($dir);
    while (false !== ($FolderOrFile = readdir($handle)))
    { 
        if($FolderOrFile != "." && $FolderOrFile != "..") { 
            if (is_dir("$dir/$FolderOrFile")) { 
                removeDir("$dir/$FolderOrFile"); 
            } else {
                @unlink("$dir/$FolderOrFile"); 
            }
        } 
    }
    closedir($handle);
}


阅读 205+

一片空白

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。