[\W]+ will match any non-word character.
but to match only the characters from the question, use this:
$string="sadw$"
if(preg_match("/[\[^\'£$%^&*()}{@:\'#~?><>,;@\|\\\-=\-_+\-¬\`\]]/", $string)){
//this string contain atleast one of these [^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`] characters
}
Answer from Trigger Eugene on Stack Overflow[\W]+ will match any non-word character.
but to match only the characters from the question, use this:
$string="sadw$"
if(preg_match("/[\[^\'£$%^&*()}{@:\'#~?><>,;@\|\\\-=\-_+\-¬\`\]]/", $string)){
//this string contain atleast one of these [^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`] characters
}
Use preg_match. This function takes in a regular expression (pattern) and the subject string and returns 1 if match occurred, 0 if no match, or false if an error occurred.
$input = 'foo';
$pattern = '/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/';
if (preg_match($pattern, $input)){
// one or more matches occurred, i.e. a special character exists in $input
}
You may also specify flags and offset for the Perform a Regular Expression Match function. See the documentation link above.
You're trying to match HÓWL instead of Hówl..
$data = "'us/Llane/Hówl' then some other text then 'us/Casey/Hówl' and so on";
preg_match_all("~us/(.*?)/Hówl~", $data, $output);
print_r($output[1]);
Output
Array
(
[0] => Llane
[1] => Casey
)
Alternatively, unless you know that Hówl will always be on the right side of the forward slash I would consider using the full Letter Unicode property \p{L}. This will allow you to match accented characters as well.
preg_match_all("~us/(.*?)/\p{L}+~u", $data, $output);
Case-Insensitivity May Not Have Worked Properly
Use this:
$regex = '~us/\K.*?(?=/Hówl)~';
$count = preg_match_all($regex, $yourstring, $matches);
if($count) print_r($matches[0]);
The matches:
Llane
Casey
See the matches in the demo.
Explanation
us/matches literal chars- The
\Ktells the engine to drop what was matched so far from the final match it returns .*?lazily matches up to...- A point where the lookahead
(?=/Hówl)can assert that what follows isHówl
This pattern would allow all characters that's not a digit or a-Z.
[^\da-zA-Z]
Regarding the \W it's a negated \w, which is the same as [A-Za-z0-9_]. Thus will \W be all characters that's not an english letter, digit or an underscore.
As I mentioned as a comment this is a great resource for learning regex. And here's a good site to test the regex.
In case you want to match on special characters
preg_match('/[\'\/~`\!@#\
input)
If I correctly understand you want only this characters - backward slash,forward slash,single quote,double quotes to be disabled for you password validation. So here is the code:
public function check_password($str){
return ( preg_match('/^[^\\\"\'\/]+$/i', $str));
}
Try like this,
public function check_password($str){
return (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#
/', $str)) ? FALSE : TRUE;
}
Use this preg_match code to only allow Letters (including uppercase), Numbers, and Spaces:
$Passed = 0;
$username = $_POST['username'];
$password = $_POST['password'];
if(!preg_match("/[^a-z0-9 ]/i", $username)){
$Passed = 1;
//stop header location here.
}
else{
$message = "Your username may only contain letters, numbers and spaces";
$_SESSION['error'] = $message;
header("Location:auth.php");
}
if ($Passed == 0){
header("Location:index.php");
}
About your original question:
This regular expression doesn't work properly due to caret (^) position:
/[^a-zA-Z0-9[:space:]]+$/
↑
In this position, caret negate following pattern inside square brackets. In fact, your pattern search for any not a-zA-Z0-9....
To match a string with only alphanumeric characters and spaces you have to move the caret at start of pattern. In this position the caret means “start of string”:
/^[a-zA-Z0-9[:space:]]+$/
↑
But you can also simplify your pattern, and replace [:space:] with a real blank space ([:space:] and \s match also newline, tab, etc...1). Try this regular expression:
/^[A-z0-9 ]+$/
Your script still not working:
The solution is die().
If the string doesn't match the pattern, you execute this code:
$message = "Your username may only contain letters, numbers and spaces";
$_SESSION['error'] = $message;
header("Location:auth.php");
Sending headers doesn't interrupt the script, so the remaining code is executed and the last sent header (Location:index.php) is loaded.
Force script termination after sending header:
header("Location:auth.php");
die();
1 From PHP documentation: “The "whitespace" characters are HT (9), LF (10), FF (12), CR (13), and space (32). However, if locale-specific matching is happening, characters with code points in the range 128-255 may also be considered as whitespace characters, for instance, NBSP (A0).”