PHP - 一串特殊字符的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13970412/
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 - Regex for a string of special characters
提问by Stann0rz
Morning SO. I'm trying to determine whether or not a string contains a list of specific characters.
早上好。我正在尝试确定字符串是否包含特定字符列表。
I know i should be using preg_matchfor this, but my regex knowledge is woeful and i have been unable to glean any information from other posts around this site. Since most of them just want to limit strings to a-z, A-Z and 0-9. But i do want some special characters to be allowed, for example: ! @ £and others not in the below string.
我知道我应该为此使用preg_match,但我的正则表达式知识很糟糕,我无法从本网站的其他帖子中收集任何信息。由于他们中的大多数只想将字符串限制为 az、AZ 和 0-9。但我确实希望允许一些特殊字符,例如:! @ £和其他不在下面的字符串中。
Characters to be matched on: # $ % ^ & * ( ) + = - [ ] \ ' ; , . / { } | \ " : < > ? ~
要匹配的字符: # $ % ^ & * ( ) + = - [ ] \ ' ; , . / { } | \ " : < > ? ~
private function containsIllegalChars($string)
{
return preg_match([REGEX_STRING_HERE], $string);
}
I originally wrote the matching in Javascript, which just looped through each letter in the string and then looped through every character in another string until it found a match. Looking back, i can't believe i even attempted to use such an archaic method. With the advent of json (and a rewrite of the application!), i'm switching the match to php, to return an error message via json.
我最初用 Javascript 编写匹配,它只是遍历字符串中的每个字母,然后遍历另一个字符串中的每个字符,直到找到匹配项。回想起来,我不敢相信我什至尝试使用这样一种古老的方法。随着 json 的出现(以及对应用程序的重写!),我将匹配切换到 php,以通过 json 返回错误消息。
I was hoping a regex guru could assist with converting the above string to a regex string, but any feedback would be appreciated!
我希望正则表达式大师可以协助将上述字符串转换为正则表达式字符串,但任何反馈将不胜感激!
回答by Touki
Regexp for a "list of disallowed character" is not mandatory.
“禁止字符列表”的正则表达式不是强制性的。
You may have a look at strpbrk. It should do the job you need.
你可以看看strpbrk。它应该可以完成您需要的工作。
Here's an example of usage
这是一个使用示例
$tests = array(
"Hello I should be allowed",
"Aw! I'm not allowed",
"Geez [another] one",
"=)",
"<WH4T4NXSS474K>"
);
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";
foreach ($tests as $test) {
echo $test;
echo ' => ';
echo (false === strpbrk($test, $illegal)) ? 'Allowed' : "Disallowed";
echo PHP_EOL;
}
回答by Arnold Daniels
return preg_match('/[#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\]/', $string);
回答by Dan Lugg
$pattern = preg_quote('#$%^&*()+=-[]\';,./{}|\":<>?~', '#');
var_dump(preg_match("#[{$pattern}]#", 'hello world')); // false
var_dump(preg_match("#[{$pattern}]#", 'he||o wor|d')); // true
var_dump(preg_match("#[{$pattern}]#", '$uper duper')); // true
Likely, you can cache the $pattern, depending on your implementation.
可能,您可以缓存$pattern,具体取决于您的实现。
(Though looking outside of regular expressions, you're best of with strpbrkas mentioned here too)
(虽然在正则表达式之外,你也最喜欢strpbrk这里提到的)
回答by Iwnnay
I think what you're looking for can be greatly simplified by including the characters that you want to allow like so:
我认为通过包含您想要允许的字符可以大大简化您正在寻找的内容:
preg_match('/[^\w!@£]/', $string)
Here's a quick breakdown of what's happening:
这是正在发生的事情的快速细分:
- [^] = not included
- \w = letters and numbers
- ! @ £ = the list of characters you would also like to allow
- [^] = 不包括
- \w = 字母和数字
- !@ £ = 您还想允许的字符列表

