search
Homephp教程php手册php一些公用函数的集合

php一些公用函数的集合

Jun 13, 2016 pm 12:29 PM
clienthttpphpfunctionaddressclientofgetgather

/*获得客户端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);
}

/*验证IP地址函数*/
function checkIP($ip) {
        return preg_match((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?);
}

/*用户输入内容过滤函数*/
function getStr($str) {
    $tmpstr = trim($str);
    $tmpstr = strip_tags($tmpstr);
    $tmpstr = htmlspecialchars($tmpstr);

    /*加入字符转义*/
    $tmpstr = addslashes($tmpstr);

    return $tmpstr;
}

/*容量大小计算函数*/
function sizecount($filesize) {
        if($filesize >= 1073741824) {
                $filesize = round($filesize / 1073741824 * 100) / 100 . ' G';
        } elseif($filesize >= 1048576) {
                $filesize = round($filesize / 1048576 * 100) / 100 . ' M';
        } elseif($filesize >= 1024) {
                $filesize = round($filesize / 1024 * 100) / 100 . ' K';
        } else {
                $filesize = $filesize . ' bytes';
        }
        return $filesize;
}

/*简单防SQL注入函数*/
function getSQL($feild) {
    $tmpfeild = mysql_escape_string($feild);

    return $tmpfeild;
}
/*$num必须为英文字符或数字0-9*/
function getNums($num) {
    return (ctype_alnum($num));
}

/*$char必须为英文字符*/
function getChar($char) {
    return (ctype_alpha($char));
}
/*匹配qq(5-12)位*/
function getQQ($qq) {
    return preg_match("/^\b[0-9]{5,12}\b/",$qq);
}
/*匹配电子邮件地址*/
function getEmail($email) {
    return strlen($email)>6 && preg_match("/^\w+@(\w+\.)+[com]|[cn]$/" , $email);
// preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/",$email);
}

/*生成email连接*/
function emailconv($email,$tolink=1) {
        $email=str_replace(array('@','.'),array('@','.'),$email);
        return $tolink ? ''.$email.'':$email;
}

/*检查ip是否被允许访问*/
function ipaccess($ip,$accesslist) {
        return preg_match("/^(".str_replace(array("\r\n",' '),array('|',''),preg_quote($accesslist,'/')).")/",$ip);
}

/*若标题过长,此函数可显示前几个字符,剩余字符用...代替*/
function cutstr($string, $length) {
    if(strlen($string) > $length) {
        for($i = 0; $i                    /*返回字符的序数值*/
            $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
        }
        return $strcut.' ...';
    } else {
        return $string;
    }
}

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.