php一些公用函数的集合

前端之家收集整理的这篇文章主要介绍了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]?\d\d?).){3}(2[0-4]\d|25[0-5]|[01]?\d\d?);
} /用户输入内容过滤函数/
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/1073741824100)/100.'G';
}elseif($filesize>=1048576){
$filesize=round($filesize/1048576
100)/100.'M';
}elseif($filesize>=1024){
$filesize=round($filesize/1024100)/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?'<ahref="mailto:'.$email.'">'.$email.'':$email;
} /
检查ip是否被允许访问/
functionipaccess($ip,$accesslist){
returnpreg_match("/^(".str_replace(array("\r\n",''),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;
}
}

猜你在找的PHP相关文章