1
$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?

codaddict
  • 445,704
  • 82
  • 492
  • 529
sam
  • 11
  • 1

1 Answers1

1

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

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • +1, and a virtual +1 for correctly escaping the backslash, but you don't have to escape any of those other characters: http://www.ideone.com/O0t35 – Alan Moore Nov 29 '10 at 00:22