sam
sam

Reputation: 11

php: preg_match: stop searching if any element of pattern exists within text

$pattern = '/^(^[\`\~\@\#\$\%\^\&\\])+$/';
if(preg_match($pattern, $textToSearch)){
   exit('Bad text.');
}

The code above is supposed to exit upon the first occurrence of any element in the pattern above. But it is not working. Will anyone please help me arrive at a working code sample?

Upvotes: 1

Views: 495

Answers (1)

codaddict
codaddict

Reputation: 455470

Since any occurrence of any of the listed special characters should mark the input as bad, you can make use of the regex: [\`\~\@\#\$\%\^\&\\\\]:

$pattern = '/[\`\~\@\#\$\%\^\&\\\\]/';
if(preg_match($pattern, $textToSearch)){
   exit('Bad text.');
}

Ideone link

Upvotes: 1

Related Questions