PHP中Skype名称的正则表达式 [英] Regular Expressions For skype name in PHP
问题描述
我想在 PHP 中使用正则表达式验证 Skype 名称.
Please I want to validate the skype name using regular expressions in PHP.
注意:它必须在 6-32 个字符之间,以字母开头并且只包含字母和数字(没有空格或特殊字符).
NOTE: It must be between 6-32 characters, start with a letter and contain only letters and numbers (no spaces or special characters).
推荐答案
这个模式应该适合你:
[a-zA-Z][a-zA-Z0-9\.,\-_]{5,31}
这将匹配一个前导字母,后跟任何字母数字组合,最多为 6 - 32 个字符(对于整个字符串).
This will match a leading letter, followed by any alpha-numeric combination up to a total of between 6 - 32 characters (for the entire string).
您可以在 PHP 中使用它:
You can use this in PHP with:
if (preg_match('/^[a-z][a-z0-9\.,\-_]{5,31}$/i', $name)) {
// you have a valid name!
}
注意,在 preg_match()
中,我添加了 i
正则表达式选项以忽略大小写.此外,我用 ^
引导模式以表示模式必须从字符串的 begining 开始,并以 $
结束表示模式必须在字符串的结尾结束.
Note, in the preg_match()
, I added the i
regex option to ignore case. Also, I lead the pattern with ^
to signify that the pattern has to start at the beginning of the string and I ended with a $
to signify that the pattern has to finish at the end of the string.
这篇关于PHP中Skype名称的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!