$users = [ '梅子16352_aa', 'www', 'eeeee.', 'ee-', 'goodman', ]; // 支持4-20位大小写字母和下划线 function isUsername($string) { return preg_match('/^[A-Za-z0-9_]{4,20}$/', $string); } foreach ($users as $user) { var_dump(isUsername($user)); } /** int(0) int(0) int(0) int(0) int(1) */