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 OverflowRegexp 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
return preg_match('/[#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/', $string);
It works as it should.
You should only add \ before * to escape it.
Check it out here: Regular expression test
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."
//]
The regular expression you are looking for is
^[a-z A-Z0-9\/\\.'"]+$
Remember if you are using PHP you need to use \ to escape the backslashes and the quotation mark you use to encapsulate the string.
In PHP using preg_match it should look like this:
preg_match("/^[a-z A-Z0-9\\/\\\\.'\"]+$/",$value);
This is a good place to find the regular expressions you might want to use. http://regexpal.com/
You can always escape them by appending a \ in front of the special characters.
Only the characters listed on this page need to be escaped in PHP regex matching/replacing.
While < and > can act as delimiter, it doesn't need to be escaped in the given example because you already have /(slash) acting as a delimiter.
Referring to the link in question
The
preg_quote()function may be used to escape a string for injection into a pattern and its optional second parameter may be used to specify the delimiter to be escaped.
< and > aren't meta characters is most contexts.
However they are used as such for:
- named capture groups
(?P<name>) - lookbehind assertions
(?<=...)
So that's why preg_quote plays it safe and escapes them. It's arguably redundant, since escaping ( and ? would be sufficient. But it doesn't hurt either.