站长资源网络编程
php一些公用函数的集合
简介/*获得客户端ip地址*/ function getIP() { if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown")) { $ip = ge
/*获得客户端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;
}
}
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;
}
}
上一篇:php5中类的学习
下一篇:PHP之变量、常量学习笔记