Home > Backend Development > PHP Tutorial > A collection of some common functions in php_PHP tutorial

A collection of some common functions in php_PHP tutorial

WBOY
Release: 2016-07-21 15:53:06
Original
922 people have browsed it

/*获得客户端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]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?);
}

/*用户输入内容过滤函数*/
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);
}

/*Generate email connection*/
function emailconv($email,$tolink=1) {
         $email=str_replace(array('@','.'),array(' @','.'),$email);
return $tolink ? ''.$email.'': $ Email;
}

/*Check whether the IP is allowed to access*/
Function iPaccess ($ IP, $ Accesslist) {
Return Preg_match ("/^(" (". Str_replace ( array("rn",' '),array('|',''),preg_quote($accesslist,'/')).")/",$ip);
}

/*If the title is too long, this function can display the first few characters and replace the remaining characters with...*/
function cutstr($string, $length) {
if(strlen($string) > $length) {
for($i = 0; $i < $length - 3; $i++) {
/*Returns the ordinal value of the character*/
$strcut .= ord($string [$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; 🎜> } else {
return $string;
}
}



http://www.bkjia.com/PHPjc/318764.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318764.htmlTechArticle/*Get the client ip address*/ functiongetIP(){ if(getenv("HTTP_CLIENT_IP")strcasecmp(getenv ("HTTP_CLIENT_IP"),"unknown")){ $ip=getenv("HTTP_CLIENT_IP"); } elseif(getenv("HTTP_X_FORWAR...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template