Home > Backend Development > PHP Tutorial > PHP regular matches 6 to 16 character combinations (and can only be numbers, letters, and underscores)

PHP regular matches 6 to 16 character combinations (and can only be numbers, letters, and underscores)

WBOY
Release: 2016-07-25 08:57:53
Original
3290 people have browsed it
This article introduces how to use PHP regular rules to match 6-digit and 16-digit character combinations. This regular rule requires only numbers, letters, and underscores. Friends in need can refer to it.

Requirements: PHP regular matches strings with 6 to 16 characters. Only 6 to 16 characters consisting of numbers, letters, and underscores are allowed. True is returned if applicable, otherwise false is returned.

Answer: From 6 to 16 digits, the regular expression can be written like this: {6,16}. The regular expression for any character from 6 to 16 bits is as follows: .{6,16} Only combinations of numbers, letters, and underscores are allowed. The regular rule is: [0-9_a-zA-Z] Put it together, the complete regex is:

^[_0-9a-z]{6,16}$

The following is an example of applying this regular verification password.

<?php
/**
* php正则验证密码规则
* 只允许 数字、字母、下划线
* 最短6位、最长16位
* 搜集整理:bbs.it-home.org
*/
function ispassword($str) {
 if (preg_match('/^[_0-9a-z]{6,16}$/i',$str)){
  return true;
 }else {
  return false;
 }
}
$password = 'abcde@';
if(ispassword($password)) {
 echo '符合';
}else {
 echo '不符合';
}
//output  不符合

echo '<br>';
$password = 'abcdeasdas_1324';
if(ispassword($password)) {
 echo '符合';
}else {
 echo '不符合';
}
//output  符合
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template