Regexp for a "list of disallowed character" is not mandatory.

You may have a look at strpbrk. It should do the job you need.

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;
}

http://codepad.org/yaJJsOpT

Answer from Touki on Stack Overflow
🌐
PHP
php.net › manual › en › function.preg-quote.php
PHP: preg_quote - Manual
To escape characters with special meaning, like: .-[]() and so on, use \Q and \E. For example: <?php echo ( preg_match('/^'.( $myvar = 'te.t' ).'$/i', 'test') ? 'match' : 'nomatch' ); ?> Will result in: match But: <?php echo ( preg_match('/^\Q'.( ...
🌐
O'Reilly
oreilly.com › library › view › php-in-a › 0596100671 › ch15s03.html
15.3. Regexp Special Characters - PHP in a Nutshell [Book]
October 13, 2005 - Regexp Special CharactersThe metacharacters +, *, ?, and { } affect the number of times a pattern should be matched, () allows you to create subpatterns, and $ and ^ affect the... - Selection from PHP in a Nutshell [Book]
Author: Paul Hudson
Published: 2005
Pages: 372
🌐
TutorialsPoint
tutorialspoint.com › article › php-program-to-check-if-a-string-has-a-special-character
PHP program to check if a string has a special character
August 17, 2020 - The following example demonstrates how to check for special characters in a string − · <?php function check_string($my_string){ $regex = preg_match('/[@_!#$%^&*()<>?\/|}{~:]/', $my_string); if($regex) print("String has special characters"); else print("String has no special characters"); } $my_string = 'This_is_$_sample!'; check_string($my_string); ?> String has special characters ·
🌐
SitePoint
sitepoint.com › php
Regex for Special Characters - PHP - SitePoint Forums | Web Development & Design Community
July 5, 2012 - // Check for Special-Character. if (empty($errors)){ if (!preg_match("#[\\~\\`\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\_\\-\\+\\=\\{\\}\\[\\]\\|\\:\\;\\ \\.\\?\\/\\\\\\\\]+#", $newPass1)){ $errors['newPass'] = 'Password must have at least 1 Special ...
🌐
BrainBell
brainbell.com › php › escaping-special-characters-in-regular-expressions.html
Escaping special characters in regular expressions in PHP – BrainBell
Escaping the special meaning of a character is done with the backslash character as with the expression "2\+3", which matches the string "2+3". If the + isn’t escaped, the pattern matches one or many occurrences of the character 2 followed by the character 3. ... <?php $string = 'Hi, 2+3 ...
🌐
DocStore
docstore.mik.ua › orelly › webprog › pcook › ch13_09.htm
Escaping Special Characters in a Regular Expression (PHP Cookbook)
You want to have characters such as * or + treated as literals, not as metacharacters, inside a regular expression. This is useful when allowing users to type in search strings you want to use inside a regular expression · Use preg_quote( ) to escape Perl-compatible regular-expression ...
🌐
Designcise
designcise.com › web › tutorial › how-to-escape-regular-expression-special-characters-in-php
How to Escape Regex Special Chars in PHP? - Designcise
June 4, 2021 - You can use the backslash character (i.e. \) to escape the regular expression special characters. For example: $str = 'hello?'; $replaceWith = 'hey?'; $pattern = '/hello\?/'; echo preg_replace($pattern, $replaceWith, $str); // 'hey?'
🌐
Medium
medium.com › @mena.meseha › the-most-commonly-used-php-regular-expression-collection-d60e8b62f21e
The most commonly used PHP regular expression collection | by Mina Ayoub | Medium
September 6, 2018 - In order for PHP to interpret, you must add “\” in front of these characters and escape some characters. Don’t forget that the characters in the brackets are exceptions to this rule — in the brackets, all special characters, including (“ ), they will lose their special nature “[*+?{}.]” matches strings containing these characters: Also, as regx’s manual tells us: “If the list contains ‘]’, it is best to put it As the first character in the list (may be followed by ‘^’).
Find elsewhere
Top answer
1 of 2
9

It works as it should.

You should only add \ before * to escape it.

Check it out here: Regular expression test

2 of 2
1

You can use this function I made sometime ago for passwords. You can use it for any string by modifying the if coniditions. Put each special characters with a \ before. It also has a check for string to be 8-20 characters long

    function isPasswordValid($password){
            $whiteListed = "\$\@\#\^\|\!\~\=\+\-\_\.";
            $status = false;
            $message = "Password is invalid";
            $containsLetter  = preg_match('/[a-zA-Z]/', $password);
            $containsDigit   = preg_match('/\d/', $password);
            $containsSpecial = preg_match('/['.$whiteListed.']/', $password);
            $containsAnyOther = preg_match('/[^A-Za-z-\d'.$whiteListed.']/', $password);
            if (strlen($password) < 8 ) $message = "Password should be at least 8 characters long";
            else if (strlen($password) > 20 ) $message = "Password should be at maximum 20 characters long";
            else if(!$containsLetter) $message = "Password should contain at least one letter.";
            else if(!$containsDigit) $message = "Password should contain at least one number.";
            else if(!$containsSpecial) $message = "Password should contain at least one of these ".stripslashes( $whiteListed )." ";
            else if($containsAnyOther) $message = "Password should contain only the mentioned characters";
            else {
                $status = true;
                $message = "Password is valid";
            }
            return array(
                "status" => $status,
                "message" => $message
            );
    }

Output

$password = "asdasdasd"
print_r(isPasswordValid($password));
// [
//   "status"=>false,
//   "message" => "Password should contain at least one number."
//]

$password = "asdasd1$asd"
print_r(isPasswordValid($password));
// [
//   "status"=>true,
//   "message" => "Password is valid."
//]
🌐
David Walsh
davidwalsh.name › letters-regex
Match Special Letters with PHP Regular Expressions
August 24, 2017 - Learn how to use the "\pL" sequence and the "u" PHP regular expression modifier to match foreign letters like à, é, ü, and others.
🌐
Code.mu
code.mu › en › php › book › prime › regular › special-characters-list
List of Special Characters in Regex in PHP
Write a regex that will find the strings '*q+', '*qq+', '*qqq+', without capturing the others.
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-regular-expressions
PHP | Regular Expressions - GeeksforGeeks
July 12, 2025 - POSIX Regular Expressions: Some regular expressions in PHP are like arithmetic expressions which are called POSIX regular expressions. Some times, complex expression are created by combining various elements or operators in regular expressions. The very basic regex is the one which matches a single character. Lets look into some of the POSIX regular expressions. Quantifiers in Regular Expression: Quantifiers are special characters which tell the quantity, frequency or the number of instances or occurrence of bracketed character or group of characters.
🌐
O'Reilly
oreilly.com › library › view › php-cookbook › 1565926811 › ch13s09.html
13.8. Escaping Special Characters in a Regular Expression - PHP Cookbook [Book]
November 20, 2002 - You want to have characters such as * or + treated as literals, not as metacharacters, inside a regular expression.
Authors: David SklarAdam Trachtenberg
Published: 2002
Pages: 640
🌐
Regular-Expressions.info
regular-expressions.info › php.html
Using Regular Expressions with PHP
In PHP, this becomes preg_match('/regex/', $subject). When forward slashes are used as the regex delimiter, any forward slashes in the regular expression have to be escaped with a backslash. So https://www\.regexp\.info/ becomes '/https:\/\/www\.regexp\.info\//'. Just like Perl, the preg functions allow any non-alphanumeric character ...
🌐
Tutorial Republic
tutorialrepublic.com › php-tutorial › php-regular-expressions.php
Regular Expressions in PHP - Tutorial Republic
The characters that are given special meaning within a regular expression, are: . * ? + [ ] ( ) { } ^ $ | \. You will need to backslash these characters whenever you want to use them literally.