PHP常用方法 3-16
/**
* 检查手机格式,中国手机不带国家代码,国际手机号格式为:国家代码-手机号
* @param $mobile
* @return bool
*/
function check_mobile($mobile)
{
if (preg_match('/(^(13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{8})$/', $mobile)) {
return true;
} else {
if (preg_match('/^\d{1,4}-\d{5,11}$/', $mobile)) {
if (preg_match('/^\d{1,4}-0+/', $mobile)) {
//不能以0开头
return false;
}
return true;
}
return false;
}
}
/**
* 判断是否SSL协议
* @return boolean
*/
function is_ssl()
{
if (isset($_SERVER['HTTPS']) && ('1' == $_SERVER['HTTPS'] || 'on' == strtolower($_SERVER['HTTPS']))) {
return true;
} elseif (isset($_SERVER['SERVER_PORT']) && ('443' == $_SERVER['SERVER_PORT'])) {
return true;
}
return false;
}
/**
* 获取惟一订单号
* @return string
*/
function get_order_sn()
{
return date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}
/**
* 判断是否为iPad访问
* @return boolean
*/
function is_ipad()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
{
return true;
}
return false;
}
}
/**
* 判断是否为iPhone访问
* @return boolean
*/
function is_iphone()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone')) {
{
return true;
}
return false;
}
}
/**
* 判断是否为ios访问
* @return boolean
*/
function is_ios()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
{
return true;
}
return false;
}
}
/**
* 判断是否为Android访问
* @return boolean
*/
function is_android()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false) {
return true;
}
return false;
}
/**
* 判断是否为微信访问
* @return boolean
*/
function is_wechat()
{
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
return true;
}
return false;
}
/**
* 判断是否为手机访问
* @return boolean
*/
function is_mobile()
{
static $cmf_is_mobile;
if (isset($cmf_is_mobile))
return $cmf_is_mobile;
$cmf_is_mobile = request()->isMobile();
return $cmf_is_mobile;
}
/**
* 随机字符串生成
* @param int $len 生成的字符串长度
* @return string
*/
function random_string($len = 6)
{
$chars = [
"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
];
$charsLen = count($chars) - 1;
shuffle($chars); // 将数组打乱
$output = "";
for ($i = 0; $i < $len; $i++) {
$output .= $chars[mt_rand(0, $charsLen)];
}
return $output;
}