Reputation: 5096
<?
$s = '';
$string1 = '<script>';
$string2 = 'färsk';
$string3 = 'öde';
$rex = "/[\^<@\/\{\}\!\*\$%\?=≤>€:\|;#]+/i";
if (preg_match($rex,$string1)) { $s = NULL; }
if ($s === NULL) { echo 'null'; } else { echo "not null"; }
?>
Why do $string2 give null? What's so special about the 'ä' character? The other Swedish ones, å and ö pass through the regex well.
Appreciate any fix to my regex. I want it to allow everything except the special chars defined in that regex.
EDIT:
Clarification: What I want to do is to let anything but a list of special characters through. $string2 and 3 should both go through, $string 1 shouldn't (because of the < and >).
Upvotes: 2
Views: 301
Reputation: 785058
Avoid unnecessary escaping. See code below:
$string1 = '<script>';
$string2 = 'färsk';
$string3 = 'öde';
$rex = "/^[^<>@\/{}!*$%?=≤€:|;#]+$/u";
var_dump (preg_match($rex, $string1)); // false
var_dump (preg_match($rex, $string2)); // true as no special char in character class
var_dump (preg_match($rex, $string3)); // true as no special char in character class
Upvotes: 3