Set of some common functions in php

Source: Internet
Author: User

/* Obtain the Client IP Address */
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 address verification function */
Function checkIP ($ ip ){
Return preg_match (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?) \.) {3} (2 [0-4] \ d | 25 [0-5] | [01]? \ D ?);
}

/* User input content filtering function */
Function getStr ($ str ){
$ Tmpstr = trim ($ str );
$ Tmpstr = strip_tags ($ tmpstr );
$ Tmpstr = htmlspecialchars ($ tmpstr );

/* Add character Escaping */
$ Tmpstr = addslashes ($ tmpstr );

Return $ tmpstr;
}

/* Capacity and size calculation function */
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;
}

/* Simple anti-SQL Injection function */
Function getSQL ($ feild ){
$ Tmpfeild = mysql_escape_string ($ feild );

Return $ tmpfeild;
}
/* $ Num must be an English character or number 0-9 */
Function getNums ($ num ){
Return (ctype_alnum ($ num ));
}

/* $ Char must be an English character */
Function getChar ($ char ){
Return (ctype_alpha ($ char ));
}
/* Match qq (5-12) digits */
Function getQQ ($ qq ){
Return preg_match ("/^ \ B [0-9] {5, 12} \ B/", $ qq );
}
/* Match the email address */
Function getEmail ($ email ){
Return strlen ($ email)> 6 & preg_match ("/^ \ w + @ (\ w + \.) + [com] | [cn] $/", $ email );
// Preg_match ("/^ [\ w \-\.] + @ [\ w \-\.] + (\. \ w +) + $/", $ email );
}

/* Generate an email connection */
Function emailconv ($ email, $ tolink = 1 ){
$ Email = str_replace (array ('@', '.'), array ('@', '.'), $ email );
Return $ tolink? '<A href = "mailto:'. $ email. '">'. $ email. '</a>': $ email;
}

/* Check whether the ip address is accessible */
Function ipaccess ($ ip, $ accesslist ){
Return preg_match ("/^ (". str_replace (array ("\ r \ n", ''), array ('|',''), preg_quote ($ accesslist ,'/')). ")/", $ ip );
}

/* If the title is too long, this function can display the first few characters. The remaining characters are replaced */
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]) & gt; 127? $ String [$ I]. $ string [++ $ I]: $ string [$ I];
}
Return $ strcut .'...';
} Else {
Return $ string;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.