PHP中一些有用的函数

<?php
 /** 
 * 加密解密
 * 
 * @param string $key
 * @param string $string
 * @param string $decrypt
 * @return string 
 */  

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;
}

 /** 
 * 获取扩展名
 * @param string $filename 
 * @return string 
 */ 
function getExtension($filename)
{
    $ext = substr($filename, strrpos($filename, '.'));
    return str_replace('.','',$ext);
}

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]);  
    }
}

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 />";
            }
        }
    }
}

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!";
    }
}

 /** 
 * 获取IP地址 
 * 
 * @return string 
 */ 
function getIp()
{
    if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")){
        $ip = getenv("HTTP_CLIENT_IP");
    }elseif (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    }elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")){
        $ip = getenv("REMOTE_ADDR");
    }elseif (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 'Illegal char';
        exit;
    } else {
        return $sql_str;
    }
}

 /** 
 * 是否是手机号码 
 * 
 * @param string $phone 手机号码 
 * @return boolean 
 */  
function isPhone($phone) {  
    if (strlen ( $phone ) != 11 || ! preg_match ( '/^1[3|4|5|8][0-9]\d{4,8}$/', $phone )) {  
        return false;  
    } else {  
        return true;  
    }  
}  
/** 
 * 验证字符串是否为数字,字母,中文和下划线构成 
 * @param string $username 
 * @return bool 
 */  
function isString($str){  
    if(preg_match('/^[\x{4e00}-\x{9fa5}\w_]+$/u',$str)){  
        return true;  
    }else{  
        return false;  
    }  
}  
/** 
 * 是否为一个合法的email 
 * @param sting $email 
 * @return boolean 
 */  
function isEmail($email){  
    if (filter_var ($email, FILTER_VALIDATE_EMAIL )) {  
        return true;  
    } else {  
        return false;  
    }  
}  
/** 
 * 是否为一个合法的url 
 * @param string $url 
 * @return boolean 
 */  
function isUrl($url){  
    if (filter_var ($url, FILTER_VALIDATE_URL )) {  
        return true;  
    } else {  
        return false;  
    }  
}  
/** 
 * 是否为一个合法的ip地址 
 * @param string $ip 
 * @return boolean 
 */  
function isIp($ip){  
    if (ip2long($ip)) {  
        return true;  
    } else {  
        return false;  
    }  
}  
/** 
 * 是否为整数 
 * @param int $number 
 * @return boolean 
 */  
function isNnumber($number){  
    if(preg_match('/^[-\+]?\d+$/',$number)){  
        return true;  
    }else{  
        return false;  
    }  
}  
/** 
 * 是否为正整数 
 * @param int $number 
 * @return boolean 
 */  
function isPositiveNumber($number){  
    if(ctype_digit ($number)){  
        return true;  
    }else{  
        return false;  
    }  
}  
/** 
 * 是否为小数 
 * @param float $number 
 * @return boolean 
 */  
function isDecimal($number){  
    if(preg_match('/^[-\+]?\d+(\.\d+)?$/',$number)){  
        return true;  
    }else{  
        return false;  
    }  
}  
/** 
 * 是否为正小数 
 * @param float $number 
 * @return boolean 
 */  
function isPositiveDecimal($number){  
    if(preg_match('/^\d+(\.\d+)?$/',$number)){  
        return true;  
    }else{  
        return false;  
    }  
}  
/** 
 * 是否为英文 
 * @param string $str 
 * @return boolean 
 */  
function isEnglish($str){  
    if(ctype_alpha($str))  
        return true;  
    else  
        return false;  
}  
/** 
 * 是否为中文 
 * @param string $str 
 * @return boolean 
 */  
function isChinese($str){  
    if(preg_match('/^[\x{4e00}-\x{9fa5}]+$/u',$str))  
        return true;  
    else   
        return false;  
}  
/** 
 * 判断是否为图片 
 * @param string $file  图片文件路径 
 * @return boolean 
 */  
function isImage($file){  
    if(file_exists($file) && getimagesize($file===false)){  
        return false;  
    }else{  
        return true;  
    }  
}  
/** 
 * 是否为合法的身份证(支持15位和18位) 
 * @param string $card 
 * @return boolean 
 */  
function isCard($card){  
    if(preg_match('/^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/',$card) 
    	||preg_match('/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/'
    	,$card))  
        return true;  
    else   
        return false;  
}

/** 
 * 验证日期格式是否正确 
 * @param string $date 
 * @param string $format 
 * @return boolean 
 */  
function isDate($date,$format='Y-m-d'){  
    $t=date_parse_from_format($format,$date);  
    if(empty($t['errors'])){  
        return true;  
    }else{  
        return false;  
    }  
}

 

posted @ 2015-09-11 22:54  踏雪无痕SS  阅读(247)  评论(0编辑  收藏  举报