PHP:匹配长度的简单正则表达式? [英] PHP: Simple regular expressions to match length?

查看:45
本文介绍了PHP:匹配长度的简单正则表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个注册系统,需要使用 REGEX(并且更喜欢)检查名称/通行证等,到目前为止我得到的是:

I'm creating a registration system that needs to check the name/pass etc. with REGEX (and prefer to), what I've got so far is:

//Check so numbers aren't first, such as 00foobar
preg_match('/^(?!\d)[a-z0-9]+$/iD',$usrname);
//Just simple check
preg_match('/^[a-zA-Z0-9]+$/',$psword);

但是我必须在 IF 语句中做一些愚蠢的事情,例如:

But I have to do stupid things in IF statements like:

if strlen($psword) >30 ||if (strlen($psword) <4) ....

如何在我的两个原始正则表达式语句中实现长度检查?这会让我很高兴..

How would I impliment the length checking in my two original regular expression statements? This would make me so happy..

推荐答案

相同,但使用 \w 和 \d 表示单词和数字,但您可能还想包含基本符号,例如 %!?/... 等...

same but using the \w and \d for word and digits, but you might want also to include basic symbols like %!?/ ... etc...

preg_match('/^[\w\d]{4,30}$/',$psword);

{n,v} 之前会验证最小 n 和最大 v 元素.

the {n,v} would validate for minimum n and maximum v elements before.

like A{2,3} 会验证 AAAAA.你可以看看那里以获得更多参考

like A{2,3} would validate for AA and AAA. you can take a look there for more references

以同样的方式,如果您只想设置模式的最小值 {n,} 会这样做.例如:

On the same fashion if you want only to set the minimum of patern {n,} would do it. For example:

preg_match('/^[\w\d]{4,}$/',$psword);

这篇关于PHP:匹配长度的简单正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆