php 如何使用正则表达式检查字符串是否仅包含某些允许的字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2788727/
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
How can I check with a regex that a string contains only certain allowed characters?
提问by Chris Van Opstal
I need a special regular expression and have no experience in them whatsoever, so I am turning to you guys on this one.
我需要一个特殊的正则表达式并且没有任何经验,所以我在这个问题上求助于你们。
I need to validate a classifieds title field so it doesn't have any special characters in it, almost.
我需要验证分类广告标题字段,以便它几乎没有任何特殊字符。
Only letters and numbers should be allowed, and also the three Swedish letters ?, ?, ? (upper- or lowercase).
只允许使用字母和数字,以及三个瑞典字母 ?, ?, ? (大写或小写)。
Besides the above, these should also be allowed:
除了上述之外,还应该允许这些:
- The "&" sign.
 - Parentheses "()"
 - Mathematical signs "-", "+", "%", "/", "*"
 - Dollar and Euro signs
 - One accent signed letter: "é". // Only this one is required
 - Double quote and single quote signs.
 - The comma "," and point "." signs
 
- 标志。
 - 括弧 ”()”
 - 数学符号“-”、“+”、“%”、“/”、“*”
 - 美元和欧元符号
 - 一个带有重音符号的字母:“é”。// 只有这一项是必需的
 - 双引号和单引号。
 - 逗号“,”和点“。” 迹象
 
回答by Chris Van Opstal
Try this:
尝试这个:
^[\s\da-zA-Z??????&()+%/*$é,.'"-]*$
Breakdown:
分解:
^= matches the start of the string
^= 匹配字符串的开头
[...]*= matches any characters (or ranges) inside the brackets one or more times
[...]*= 匹配括号内的任何字符(或范围)一次或多次
$= matches the end of the string
$= 匹配字符串的结尾
Updatedwith all the suggestions from the comments. Thanks guys!
更新了评论中的所有建议。谢谢你们!
回答by Kae Cyphet
There is a security flaw in the accepted answer:
接受的答案中存在安全漏洞:
^[\s\da-zA-Z??????&()+%/*$é,.'"-]*$
This will generate a true response for empty strings as *is for 0 or more occurrences.
这将为空字符串生成一个真实的响应,就像*0 次或多次出现一样。
Here is a more secure version:
这是一个更安全的版本:
^[\s\da-zA-Z??????&()+%/*$é,.'"-]+$
The +responds true to 1 or more occurrences.
+对 1 次或多次出现的响应为 true。
More information can be found at https://regexr.com/
更多信息可以在https://regexr.com/找到
回答by dnagirl
PHP has a variety of functions that can help with text validation. You may find them more appropriate than a straight regex.  Consider  strip_tags(), htmlspecialchars(), htmlentities()
PHP 有多种功能可以帮助进行文本验证。您可能会发现它们比直接的正则表达式更合适。考虑  strip_tags(), htmlspecialchars(),htmlentities()
As well, if you are running >PHP5.2, you can use the excellent Filterfunctions, which were designed for exactly your situation.
同样,如果您运行的是 >PHP5.2,您可以使用出色的Filter函数,这些函数专为您的情况而设计。
回答by Tim Pietzcker
^[\sa-zA-Z0-9???&()+%/*$é,.'"-]*$
will match all the required characters.
将匹配所有必需的字符。
In PHP:
在 PHP 中:
if (preg_match('#^[\sa-zA-Z0-9???&()+%/*$é,.\'"-]*$#i', $subject)) {
 # Successful match
} else {
 # Match attempt failed
}
回答by Glycerine
Hey buddy, its really very very simple. Just do a regex replace of anything you don't want...
嘿伙计,它真的非常非常简单。只需做一个正则表达式替换你不想要的任何东西......
In this example - I simple say "this stuff is not allowed"
在这个例子中 - 我简单地说“这个东西是不允许的”
More specifically it says "if its not in this regex match, replace that character with an empty string.
更具体地说,它说“如果它不在此正则表达式匹配中,则将该字符替换为空字符串。
PHP:
PHP:
$result = preg_replace('#([^a-zA-Z0-9£()+=%/*$,.])#imx', '', $subject);
if the section where you have a-zA-Z0-9£()+=%/*$simply add the character your want to pass your regex and be allowed in the post.
如果您a-zA-Z0-9£()+=%/*$只是添加了想要传递正则表达式并允许在帖子中使用的字符的部分。
Edit:
编辑:
More expansive
更广阔
This vbersion of the regex contains all upper case and lower case accented characters. Their in ASCII format as I don't know the key to write them!
正则表达式的这个版本包含所有大写和小写重音字符。它们采用 ASCII 格式,因为我不知道编写它们的关键!
$result = preg_replace('#([^a-zA-Z0-9£()+=%/*$,.\x99\xBC\xBD\xBE\xC0\xC1\xC2\xC3\xC4\xC5\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xE0\xE1\xE2\xE3\xE4\xE5\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF9\xFA\xFB\xFC\xFD])#imx', '', $subject);
$result = preg_replace('#([^a-zA-Z0-9£()+=%/*$,.\x99\xBC\xBD\xBE\xC0\xC1\xC2\xC3\xC4\xC5\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD1\xD2\xD3\xD4\xD5\xD6\xE0\xE1\xE2\xE3\xE4\xE5\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF9\xFA\xFB\xFC\xFD])#imx', '', $subject);

