`

几个经典函数

    博客分类:
  • php
 
阅读更多

1:是否为邮件

function is_email($email) {
return strlen ( $email ) > 6 && preg_match ( "/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email );
}

2:是否是手机号
function is_mobile($mobile) {
return preg_match ( "/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|18[0-9]{9}$/", $mobile );
}

3:获取IP地址

function get_ip() {

if (! empty ( $_SERVER ["HTTP_CLIENT_IP"] ))
$cip = $_SERVER ["HTTP_CLIENT_IP"];
else if (! empty ( $_SERVER ["HTTP_X_FORWARDED_FOR"] ))
$cip = $_SERVER ["HTTP_X_FORWARDED_FOR"];
else if (! empty ( $_SERVER ["REMOTE_ADDR"] ))
$cip = $_SERVER ["REMOTE_ADDR"];
else
$cip = '';

return $cip;
}

4:随即生成字符串

function randomkeys($length) {
$key = null;
$pattern = '1234567890abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLOMNOPQRSTUVWXYZ';
for($i = 0; $i < $length; $i ++) {
$key .= $pattern {mt_rand ( 0, 35 )};
}
return $key;
}

5:取文件后缀名

function get_file_exts($myfilename)
{
$mypos = strrpos($myfilename, '.');
$exts = substr($myfilename, $mypos + 1, (strlen($myfilename) - $mypos - 1));
return $exts;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics