PHP regular expression to match alpha-numeric strings with some (but not all) punctuation

自闭症网瘾萝莉.ら 提交于 2019-12-24 22:37:01

问题


I've written a regular expression in PHP to allow strings that are alpha-numeric with any punctuation except & or @. Essentially, I need to allow anything on a standard American keyboard with the exception of those two characters. It took me a while to come up with the following regex, which seems to be doing what I need:

if (ereg("[^]A-Za-z0-9\[!\"#$%'()*+,./:;<=>?^_`{|}~\-]", $test_string)) {
  // error message goes here
}

Which brings me to my question... is there a better, simpler, or more efficient way?


回答1:


Have a look at character ranges:

@[!-%'-?A-~]+@

This will exclude the characters & (\0x26) and @ (0x40). Looking at an ASCII Table,you can see how this works: The exclamation mark is the first character in the ASCII set, that is not whitespace. It will then match everything up to and including the % character, which immediately precedes the ampersand. Then the next range until the @ character, which lies between ? and A. After that, we match everything unto the end of the standard ASCII character set which is a ~.

Update

To make things more readable, you might also consider to do this in two steps: At first, filter anything outside of the default ASCII range.

@[!-~]+@

In a second step, filter your undesired characters, or simply do a str_pos on the characters.

At the end, you can compare it with what you started to see whether it contained any undesired characters.

Instead, you could also use a regex such as this for the second step. /[^@&]+/

The steps are interchangeable and doing a str_pos on @ or & as a first step, to identify bad characters, may be better performance wise.




回答2:


What about this:

[^&@]

with preg_match

$str = 'a';
var_dump(preg_match('~^[^&@]+$~', $str)); // true

$str = '&';
var_dump(preg_match('~^[^&@]+$~', $str)); // false

$str = '!';
var_dump(preg_match('~^[^&@]+$~', $str)); // true



回答3:


I think rather than testing for all the alpha numeric characters you can simply check for @ and & and use a not?

$reg = '/@|&/';
if(!preg_match($reg, "YOUR STRING CAN GO HERE")){
// your code goes here
}


来源:https://stackoverflow.com/questions/4468442/php-regular-expression-to-match-alpha-numeric-strings-with-some-but-not-all-pu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!