php一些公用函數的集合

來源:互聯網
上載者:User

/*獲得用戶端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 ? '<a href="mailto: '.$email.'">'.$email.'</a>':$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 < $length - 3; $i++) {
/*返回字元的序數值*/
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
return $strcut.' ...';
} else {
return $string;
}
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.