[英]PHP regular expression for alphanumeric and hypen input
Is it possible to validate input depending on whether it only contains a combination of letters, numbers and hyphens (where a hyphen is not repeated twice-in-a-row and does not begins/ends the string)? 是否可以根据输入是否仅包含字母,数字和连字符的组合(其中连字符在行中不重复两次并且不以字符串开头/结尾)来验证输入?
Thanks to Validate username as alphanumeric with underscores 感谢验证用户名为带下划线的字母数字
I know that the following validates a string based on alphanumeric input with underscores, would it be possible to alter this? 我知道以下内容可根据带下划线的字母数字输入来验证字符串,是否可以更改此字符串?
function validate_alphanumeric_underscore($str)
{
return preg_match('/^[a-zA-Z0-9_]+$/',$str);
}
Thank you in advance for your help! 预先感谢您的帮助!
This can be done quite easily with a single regular expression: 使用单个正则表达式可以很容易地做到这一点:
/^([a-z0-9]+-)*[a-z0-9]+$/i
This meets all your criteria: 这符合您的所有条件:
Matches: 火柴:
Doesn't match: 不符合:
Assuming a minimum of 2 characters: 假设至少2个字符:
This will validate the general format (not starting or ending with a -
). 这将验证常规格式(不以-
开头或结尾)。
/^[a-z0-9][a-z0-9-]*[a-z0-9]$/i
Then add a simple check for double hyphens using strpos
(if it's false, there is no --
in the string, so we want to return true
. Otherwise, we want to return false
, so that's why the false ===
is in there): 然后使用strpos
添加一个简单的双连字符检查(如果为假,则字符串中没有--
,所以我们想返回true
。否则,我们想返回false
,所以这就是false ===
的原因):
false === strpos($string, '--');
So, you could do it as: 因此,您可以这样做:
function validateAlphaNumericUnderscore($string) {
if (0 < preg_match('/^[a-z0-9][a-z0-9-]*[a-z0-9]$/i', $string)) {
return false === strpos($string, '--');
}
return false;
}
Now, I'm sure there's a way to do it in a single regex (without needing the additional strpos
), but I'm blanking on that now. 现在,我敢肯定有一种方法可以在单个正则表达式中完成(不需要额外的strpos
),但是现在我对此空白。 This is a simple regex, and a simple second string comparison (non regex based). 这是一个简单的正则表达式,以及一个简单的第二个字符串比较(基于非正则表达式)。
Hopefully this suits your needs... 希望这能满足您的需求...
Edit: In fact, you could make this more efficient by checking for the --
first (since the string function is cheaper than the regex): 编辑:实际上,您可以通过首先检查--
提高效率(因为字符串函数比正则表达式便宜):
function validateAlphaNumericUnderscore($string) {
if (false === strpos($string, '--')
return 0 < preg_match('/^[a-z0-9][a-z0-9-]*[a-z0-9]$/i', $string);
}
return false;
}
ircmaxell's answer uses a regex and a strpos()
check, and is an answer I prefer, but here's how I did it with a single regex. ircmaxell的答案使用了正则表达式和strpos()
检查,这是我更喜欢的答案,但是这是我使用单个正则表达式来完成的方法。 Disclaimer: this has vast room for improvement : 免责声明:这有很大的改进空间 :
function validate_alphanumeric_hyphenated($str)
{
/*
* Match either one or more alphanumeric characters, or a sequence with
* a series of alphanumeric characters without consecutive, leading
* or trailing hyphens.
*
* Is probably unnecessarily long.
*/
return preg_match("/^(?:[a-zA-Z0-9]+|[a-zA-Z0-9](?:[a-zA-Z0-9]|-(?!-))*[a-zA-Z0-9])$/", $str);
}
You could do it with two regexes: 您可以使用两个正则表达式来做到这一点:
if( !preg_match('/(?:^-|-$|-{2,})/',$str) && preg_match(/^[a-zA-Z0-9_]+$/',$str) ) {
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.