Home  >  Article  >  Backend Development  >  Some commonly used functions_PHP tutorial

Some commonly used functions_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 10:59:51849browse

​ These are some functions that are used frequently, some of which come from other people’s programs...
1. Generate random string function
function random($length) {
$hash = '';
$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
$max = strlen($chars) - 1;
mt_srand((double)microtime() * 1000000);
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0, $max)];
}
return $hash;
}
2. Intercept a string of a certain length
Note: This function is valid for GB2312
function wordscut($string, $length,$sss=0) {
if(strlen($string) > $length) {
                if($sss){
               $length=$length - 3;
                 $addstr=' ...';
             }
for($i = 0; $i < $length; $i++) {
If(ord($string[$i]) > 127) {
$wordscut .= $string[$i].$string[$i + 1];
$i++;
} else {
$wordscut .= $string[$i];
}
}
Return $wordscut.$addstr;
}
return $string;
}
3. 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");

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445585.htmlTechArticleThese are some functions that are used frequently, some of which come from other people’s programs... 1. Generate random string function function random($length) { $hash = ; $chars = ABCDEFGHIJKLMNOPQRSTUVWXYZ...
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