php常用正则检测验证

php常用正则检测验证
//验证是否是合法的身份证号,简单验证
public static function isValidIdCardNo($idcard)
{
    $length = strlen($idcard);

    //15位老身份证
    if ($length == 15)
	{
        if (checkdate(substr($idcard, 8, 2), substr($idcard, 10, 2), '19' . substr($idcard, 6, 2)))
		{
            return true;
        }
    }

    //18位二代身份证号
    if ($length == 18)
	{
        if (!checkdate(substr($idcard, 10, 2), substr($idcard, 12, 2), substr($idcard, 6, 4)))
		{
            return false;
        }

        $idcard = str_split($idcard);
        if (strtolower($idcard[17]) == 'x')
		{
            $idcard[17] = '10';
        }

        //加权求和
        $sum = 0;
        //加权因子
        $wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];
        for ($i = 0; $i < 17; $i++)
		{
            $sum += $wi[$i] * $idcard[$i];
        }

        //得到验证码所位置
        $position = $sum % 11;

        //身份证验证位值 10代表X
        $code = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];
        if ($idcard[17] == $code[$position])
		{
            return true;
        }
    }

    return false;
}

//验证是否是合法的银行卡,不包含信用卡
public static function isValidBankCard($card)
{
    if (!is_numeric($card))
	{
        return false;
    }

    if (strlen($card) < 16 || strlen($card) > 19)
	{
        return false;
    }

    $cardHeader = [10, 18, 30, 35, 37, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 60, 62, 65, 68, 69, 84, 87, 88, 94, 95, 98, 99];
    if (!in_array(substr($card, 0, 2), $cardHeader))
	{
        return false;
    }

    $numShouldCheck = str_split(substr($card, 0, -1));
    krsort($numShouldCheck);

    $odd = $odd['gt9'] = $odd['gt9']['tens'] = $odd['gt9']['unit'] = $odd['lt9'] = $even = [];
    array_walk($numShouldCheck, function ($item, $key) use (&$odd, &$even, $card){

        if ((strlen($card) == 16) && (substr($card, 0, 2) == '62'))
		{
            $key += 1;
        }

        if (($key & 1))
		{
            $t = $item * 2;
            if ($t > 9)
			{
                $odd['gt9']['unit'][] = intval($t % 10);
                $odd['gt9']['tens'][] = intval($t / 10);
            }
			else
			{
                $odd['lt9'][] = $t;
            }
        }
		else
		{
            $even[] = $item;
        }
    });

    $total = array_sum($even);
    array_walk_recursive($odd, function ($item, $key) use (&$total) {
        $total += $item;
    });

    $luhm = 10 - ($total % 10 == 0 ? 10 : $total % 10);

    $lastNumOfCard = substr($card, -1, 1);
    if ($luhm != $lastNumOfCard)
	{
        return false;
    }

    return true;
}

/**
 * 检查是否是AJAX请求。
 * Check is ajax request.
 *
 * @static
 * @access public
 * @return bool
 */
public static function isAjaxRequest()
{
    if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') return true;
    if(isset($_GET['HTTP_X_REQUESTED_WITH'])    && $_GET['HTTP_X_REQUESTED_WITH']    == 'XMLHttpRequest') return true;

    return false;
}

/**
 * 检查是否是POST请求
 */
public static function isPostRequest()
{
    if($_SERVER['REQUEST_METHOD'] == 'POST') return true;
    if($_POST) return true;

    return false;
}

/**
 * 是否是GET提交的
 */
public static function isGetRequest()
{
    return $_SERVER['REQUEST_METHOD'] == 'GET' ? true : false;
}

/**
 * 获取文本中首张图片地址
 * @param  [type] $content
 * @return [type]
 */
function getfirstpic($content)
{
	if(preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|bmp|png))\\2/i", $content, $matches))
	{
		$file=$_SERVER['DOCUMENT_ROOT'].$matches[3][0];

		if(file_exists($file))
		{
			return $matches[3][0];
		}
	}
	else
	{
		return false;
	}
}

/**
 * 截取中文字符串
 * @param string $string 中文字符串
 * @param int $sublen 截取长度
 * @param int $start 开始长度 默认0
 * @param string $code 编码方式 默认UTF-8
 * @param string $omitted 末尾省略符 默认...
 * @return string
 */
function cut_str($string, $sublen=250, $omitted = '', $start=0, $code='UTF-8')
{
	$string = strip_tags($string);
	$string = str_replace(" ","",$string);
	$string = mb_strcut($string,$start,$sublen,$code);
	$string.= $omitted;
	return $string;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值