简体   繁体   English

如何显示不匹配正则表达式的字符列表?

[英]How can I display a list of characters that fail to match a regular expression?

For example, if I'm doing some form input validation and I'm using the following code for the name field. 例如,如果我正在进行某种形式的输入验证,并且正在对name字段使用以下代码。

preg_match("/^[a-zA-Z .-]$/", $firstname);

If someone types in Mr. (Awkward) Double-Barrelled I want to be able to display a message saying Invalid character(s): (, ) 如果有人输入Mr. (Awkward) Double-Barrelled我希望能够显示一条消息,指出Invalid character(s): (, )

You can fetch all occurences of characters that are not within your character-class. 您可以获取不在字符类中的所有字符。
Negate the class [...] -> [^...] and then fetch all matches. 对类[...]-> [^ ...]求反,然后获取所有匹配项。

$firstname = 'Mr. (Awkward) Double-Barrelled';

if ( 0 < preg_match_all("/[^a-zA-Z .-]+/", $firstname, $cap) ) {
  foreach( $cap[0] as $e ) {
    echo 'invalid character(s): ', htmlspecialchars($e), "\n";
  }
}

using the PREG_OFFSET_CAPTURE flag described at http://docs.php.net/preg_match_all you can even tell the user where that character is in the input. 在使用中描述的PREG_OFFSET_CAPTURE标志http://docs.php.net/preg_match_all你甚至可以告诉其中该字符是在用户输入。

edit: Or you can use preg_replace_callback() to visually mark the invalid characters somehow. 编辑:或者,您可以使用preg_replace_callback()以某种方式在视觉上标记无效字符。 eg (using an anonymous function/closure, php 5.3+) 例如(使用匿名函数/关闭,PHP 5.3+)

$firstname = 'Mr. (Awkward) Double-Barrelled';
$invalid = array();
$result = preg_replace_callback("/[^a-zA-Z .-]+/", function($c) use(&$invalid) { $invalid[] = $c[0]; return '['.$c[0].']'; }, $firstname);
if ( $firstname!==$result ) {
  echo 'invalid characters: "', join(', ', $invalid), '" in your input: ', $result;
}

prints invalid characters: "(, )" in your input: Mr. [(]Awkward[)] Double-Barrelled invalid characters: "(, )" in your input: Mr. [(]Awkward[)] Double-Barrelled打印invalid characters: "(, )" in your input: Mr. [(]Awkward[)] Double-Barrelled

您可以在输入中搜索([^a-zA-Z .-])以获取所有非法字符。

preg_match("/[^a-zA-Z0-9\\s\\.\\-]/", $text) should do the trick. preg_match("/[^a-zA-Z0-9\\s\\.\\-]/", $text)应该可以解决问题。 You're really supposed to escape the ' ', '.', and '-' characters. 您真的应该转义','。'和'-'字符。 I personally wouldn't bother wasting space to figure out which characters are invalid. 我个人不会浪费时间来找出哪些字符无效。 If the person can't figure it out based on a statement saying 'Allowed Characters: (whatever)' then there is no hope for them. 如果这个人不能根据“允许的字符:(任何)”的说法弄清楚,那么他们就没有希望了。

Here's a list of regex characters which also includes a list of characters you're supposed to escape. 这是一个正则表达式字符列表,其中还包括您应该转义的字符列表。

you can also simply replace valid characters with "nothing" - the rest, if any, will be invalid. 您也可以简单地用“ nothing”代替有效字符-其余的(如果有的话)将无效。

 $badchars = preg_replace(/[a-z0-9 .-]/, "", $input);
 if(strlen($badchars))
       error

You could split along the allowed characters: 您可以沿允许的字符分割:

$result = preg_split('/[a-zA-Z .-]+/s', $subject);

...and get a list of all the characters that remain. ...并获得所有剩余字符的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM