32

Possible Duplicate:
preg_match php special characters

Hi all, I want to check if these characters exist in a string by using preg_match:

^'£$%^&*()}{@'#~?><>,@|\-=-_+-¬'

Help please!

SharpC
  • 6,220
  • 4
  • 42
  • 39
stefanosn
  • 3,022
  • 9
  • 50
  • 74

2 Answers2

78
<?php

$string = 'foo';

if (preg_match('/[\'^£$%&*()}{@#~?><>,|=_+¬-]/', $string))
{
    // one or more of the 'special characters' found in $string
}
chigley
  • 2,452
  • 18
  • 18
8

preg_match('/'.preg_quote('^\'£$%^&*()}{@#~?><,@|-=-_+-¬', '/').'/', $string);

Petah
  • 44,167
  • 27
  • 153
  • 209
  • 1
    Didn't realize that `preg_quote` was an available function. May be useful to some. – Chad Feb 27 '19 at 16:59