/** * 验证 手机号和电话 * @param $mobile * @return bool */ public function validateMobile($mobile) { if (preg_match('/^([0-9]{3,4}-)?[0-9]{7,8}$/', $mobile) || preg_match('/^\+?[\d]{0,3}(-)?1\d{10}$/', $mobile)) { return true; } return false; } /** * 验证邮箱格式 * @param $email 被验证的邮箱 * @return bool */ public function validateEmail($email) { if (!preg_match('/^\w[-_:+*\.\w\?]+@(\w-?)+(\.\w{2,6}){1,3}$/i', $email)) { return false; } return true; } /** * 验证金钱 最多 两位小数 * @param $price * @return bool */ public function validateMoney($price) { if (preg_match('/^[0-9]+(.[0-9]{1,2})?$/', $price)) { return true; }else { return false; } } /** * 验证 最多1位小数 * @param $price * @return bool */ public function validateWorkTime($price) { if (preg_match('/^[0-9]+(.[0-9]{1})?$/', $price)) { return true; } else { return false; } } /** * 验证 正整数 * @param $keyword * @return bool */ public function validateInt($keyword) { if(preg_match("/^[1-9][0-9]*$/",$keyword)){ return true; } else { return false; } } /** * 验证日期格式: 2019-08-03 02:00:00 * @param $time * @return bool */ public function validateDatetime($time) { if(preg_match("/^\d{4}\-\d{2}\-\d{2}\s\d{2}:\d{2}:\d{2}$/", $time)){ return true; } else { return false; } }
文章来源:智云一二三科技
文章标题:常用的PHP正则表达式验证参数方法
文章地址:https://www.zhihuclub.com/148938.shtml