PHP preg_match - 只允许字母数字字符串和 - _ 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7753365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP preg_match - only allow alphanumeric strings and - _ characters
提问by Lee Price
I need the regex to check if a string only contains numbers, letters, hyphens or underscore
我需要正则表达式来检查字符串是否只包含数字、字母、连字符或下划线
$string1 = "This is a string*";
$string2 = "this_is-a-string";
if(preg_match('******', $string1){
echo "String 1 not acceptable acceptable";
// String2 acceptable
}
回答by SERPRO
Code:
代码:
if(preg_match('/[^a-z_\-0-9]/i', $string))
{
echo "not valid string";
}
Explanation:
解释:
- [] => character class definition
- ^ => negate the class
- a-z => chars from 'a' to 'z'
- _ => underscore
- - => hyphen '-' (You need to escape it)
- 0-9 => numbers (from zero to nine)
- [] => 字符类定义
- ^ => 否定类
- az => 从 'a' 到 'z' 的字符
- _ => 下划线
- - => 连字符“-”(你需要转义它)
- 0-9 => 数字(从零到九)
The 'i' modifier at the end of the regex is for 'case-insensitive' if you don't put that you will need to add the upper case characters in the code before by doing A-Z
正则表达式末尾的“i”修饰符用于“不区分大小写”,如果您不添加,则需要在执行 AZ 之前在代码中添加大写字符
回答by matino
if(!preg_match('/^[\w-]+$/', $string1)) {
echo "String 1 not acceptable acceptable";
// String2 acceptable
}
回答by Epiphany
Here is one equivalent of the accepted answer for the UTF-8 world.
这是 UTF-8 世界公认的答案的一个等价物。
if (!preg_match('/^[\p{L}\p{N}_-]+$/u', $string)){
//Disallowed Character In $string
}
Explanation:
解释:
- [] => character class definition
- p{L} => matches any kind of letter character from any language
- p{N} => matches any kind of numeric character
- _- => matches underscore and hyphen
- + => Quantifier — Matches between one to unlimited times (greedy)
- /u => Unicode modifier. Pattern strings are treated as UTF-16. Also causes escape sequences to match unicode characters
- [] => 字符类定义
- p{L} => 匹配来自任何语言的任何类型的字母字符
- p{N} => 匹配任何类型的数字字符
- _- => 匹配下划线和连字符
- + => 量词——匹配一次到无限次(贪婪)
- /u => Unicode 修饰符。模式字符串被视为 UTF-16。还会导致转义序列匹配 unicode 字符
Note, that if the hyphen is the last character in the class definition it does not need to be escaped. If the dash appears elsewhere in the class definition it needs to be escaped, as it will be seen as a range character rather then a hyphen.
请注意,如果连字符是类定义中的最后一个字符,则不需要对其进行转义。如果破折号出现在类定义的其他地方,则需要对其进行转义,因为它将被视为范围字符而不是连字符。
回答by Thielicious
回答by Anuj TBE
Why to use regex? PHP has some built in functionality to do that
为什么要使用正则表达式?PHP 有一些内置功能可以做到这一点
<?php
$valid_symbols = array('-', '_');
$string1 = "This is a string*";
$string2 = "this_is-a-string";
if(preg_match('/\s/',$string1) || !ctype_alnum(str_replace($valid_symbols, '', $string1))) {
echo "String 1 not acceptable acceptable";
}
?>
preg_match('/\s/',$username)
will check for blank space
preg_match('/\s/',$username)
将检查空格
!ctype_alnum(str_replace($valid_symbols, '', $string1))
will check for valid_symbols
!ctype_alnum(str_replace($valid_symbols, '', $string1))
将检查 valid_symbols