php一些公用函数的集合
程序员文章站
2022-05-18 10:17:51
/*获得客户端ip地址*/ function getip() { &...
/*获得客户端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;
}
}