欢迎来到福编程网,本站提供各种互联网专业知识!

php一些公用函数的集合

发布时间:2008-03-27 作者: 来源:转载
php常用公用函数
/*获得客户端ip地址*/
functiongetIP(){
if(getenv("HTTP_CLIENT_IP")&&strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown")){
$ip=getenv("HTTP_CLIENT_IP");
}
elseif(getenv("HTTP_X_FORWARDED_FOR")&&strcasecmp(getenv("HTTP_X_FORWARDED_FOR"),"unknown")){
$ip=getenv("HTTP_X_FORWARDED_FOR");
}
elseif(getenv("REMOTE_ADDR")&&strcasecmp(getenv("REMOTE_ADDR"),"unknown")){
$ip=getenv("REMOTE_ADDR");
}
elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],"unknown")){
$ip=$_SERVER['REMOTE_ADDR'];
}
else{
$ip="unknown";
}

return($ip);
}

/*验证IP地址函数*/
functioncheckIP($ip){
returnpreg_match((2[0-4]d|25[0-5]|[01]?dd?).){3}(2[0-4]d|25[0-5]|[01]?dd?);
}

/*用户输入内容过滤函数*/
functiongetStr($str){
$tmpstr=trim($str);
$tmpstr=strip_tags($tmpstr);
$tmpstr=htmlspecialchars($tmpstr);

/*加入字符转义*/
$tmpstr=addslashes($tmpstr);

return$tmpstr;
}

/*容量大小计算函数*/
functionsizecount($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注入函数*/
functiongetSQL($feild){
$tmpfeild=mysql_escape_string($feild);

return$tmpfeild;
}
/*$num必须为英文字符或数字0-9*/
functiongetNums($num){
return(ctype_alnum($num));
}

/*$char必须为英文字符*/
functiongetChar($char){
return(ctype_alpha($char));
}
/*匹配qq(5-12)位*/
functiongetQQ($qq){
returnpreg_match("/^b[0-9]{5,12}b/",$qq);
}
/*匹配电子邮件地址*/
functiongetEmail($email){
returnstrlen($email)>6&&preg_match("/^w+@(w+.)+[com]|[cn]$/",$email);
//preg_match("/^[w-.]+@[w-.]+(.w+)+$/",$email);
}

/*生成email连接*/
functionemailconv($email,$tolink=1){
$email=str_replace(array('@','.'),array('@','.'),$email);
return$tolink?''.$email.'':$email;
}

/*检查ip是否被允许访问*/
functionipaccess($ip,$accesslist){
returnpreg_match("/^(".str_replace(array("rn",''),array('|',''),preg_quote($accesslist,'/')).")/",$ip);
}

/*若标题过长,此函数可显示前几个字符,剩余字符用...代替*/
functioncutstr($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;
}
}

相关推荐