php一些公用函数的集合

时间:2021-05-26


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);
}


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/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;
}


functiongetSQL($feild){
$tmpfeild=mysql_escape_string($feild);

return$tmpfeild;
}

functiongetNums($num){
return(ctype_alnum($num));
}


functiongetChar($char){
return(ctype_alpha($char));
}

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);
}


functionemailconv($email,$tolink=1){
$email=str_replace(array('@','.'),array('@','.'),$email);
return$tolink?'<ahref="mailto:'.$email.'">'.$email.'</a>':$email;
}


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;
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章