1

I am trying to put a regex match in PHP to find whether the string contains any other invalid characters other than the following characters,

~!@#$%^&*()_-+=\}]{[::'"/>.<,
alpha, space, numeric

I want to print the string if it contains any other character other than the previously mentioned characters

ßab? - Invalid
Ba,-  - Valid

I tried using preg_match with few inputs but unable to complete it.

Any help would be appreciated.

2 Answers 2

3

Update

This expression will match the negative set of your valid range:

$valid = preg_quote('~!@#$%^&*()_-+=\}]{[::\'"/>.<,', '/');

if (preg_match("/[^$valid\d\sA-Za-z]/", $invalid)) {
        echo "invalid chars\n";
}

I'm using preg_quote() here to make sure all characters are properly escaped.

Old answer

I calculated the negative set based on your question:

if (preg_match('/[\x00-\x08\x0c\x0e-\x1f\x3b\x3f\x60\x7c\x7f-\xff]/', $str)) {
        echo "matches invalid chars\n";
}

To arrive at this set, you can use this code:

$s = '~!@#$%^&*()_-+=\}]{[::\'"/>.<,'
        . join('', range('a', 'z'))
        . join('', range('A', 'Z'))
        . join('', range('0', '9'))
        . " \t\r\v\n";

$missing = count_chars($s, 2);
print_r($missing);

It prints an array of ordinal character codes that's not inside $s; with that you can generate above pattern.

10
  • The regex seems to fail when i insert numbers
    – w00
    Mar 12, 2013 at 8:28
  • @w00 Could you give an example?
    – Ja͢ck
    Mar 12, 2013 at 8:29
  • @w00 That's funny. I misread my calculator :) fixed. 0x36 instead of 0x3b
    – Ja͢ck
    Mar 12, 2013 at 8:36
  • 1
    +1, although... @user1518659 FYI: preg_quote() is a bit of a carpet-bomb approach to the escaping here. The only characters that need to be escaped in a character class are ^\]- and the delimiter, a caret only needs to be escaped if it is the first character in the set and a hyphen only if failure to do so would indicate a character range. However, what has been done above will still work fine, and I think this is probably the best approach from a readability and maintainability perspective.
    – DaveRandom
    Mar 12, 2013 at 9:32
  • 1
    The / must be escaped too and the backslash needs to written as ` \\\\ ` ;-) too much headache!
    – Ja͢ck
    Mar 12, 2013 at 9:41
0

The below regex would match any string that contains your special characters,alpha, space, numeric..

/^[\s\w~!@#$%^&*()+=\}\]{\[:'"\/>.<,-]*$/

^ is the start of the string..$ is the end of the string..^,$ are required because they would match the input exactly instead of partially

[] is a character class which would match any one of the characters within it..

* is a quantifier which would match preceeding character,group or character class 0 to many times..

7
  • 1
    You forgot to escape the / in your valid character list. Should be \/ otherwise it won't work.
    – w00
    Mar 12, 2013 at 8:02
  • it is showing error at the position of "#"? where is the problem? Mar 12, 2013 at 8:05
  • also don't we need to escape ' and " ? Mar 12, 2013 at 8:06
  • @user1518659 i don't know about php and it's escape sequence..but it does work here(an online php regex tester)
    – Anirudha
    Mar 12, 2013 at 8:08
  • 1
    I believe you need to escape the - character aswell, as this implies a "range" of characters. Also the single quote or double quote must be escaped. And this also doesn't match for backslashes `. Try it with this regex: /^[\s\w~!@#$%^&*()+=\}]{[:\'"\/>.<,\-\\\]+$/` -- codepad.viper-7.com/NBVO1I (something is wrong with the formatting in this comment, can't get it right)
    – w00
    Mar 12, 2013 at 8:20

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.