18

How can I use preg_match to see if special characters [^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`] exist in a string?

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
stefanosn
  • 3,264
  • 10
  • 53
  • 79

7 Answers7

23

[\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
  }
Moradnejad
  • 3,466
  • 2
  • 30
  • 52
Trigger Eugene
  • 346
  • 2
  • 3
  • Thank you for your answer but i need only specific characters like ^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬` – stefanosn Oct 14 '10 at 21:40
  • 3
    Normally I'd say `[\W]` is still perfect for you, as a "word character" refers to any letter, number, or underscore and excludes just about everything else. I'm not sure if it includes hyphen. Then I noticed underscore was in the list of characters you want to check for. Since you only want to find single characters it may be quicker to use `explode()` and `in_array()` instead of `preg_match()`, or just use a `while()` loop. Although neither of these are very intuitive. – stevendesu Oct 15 '10 at 00:41
  • What if the string contains Unicode character? Your regex excludes them! – emeraldhieu Jul 28 '12 at 04:24
  • 1
    [\W] includes `#` – d.raev Feb 25 '18 at 17:51
  • @d.raev that is expected for it to include # as a non-word character – Jacob Smith Mar 30 '19 at 05:50
14

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.

ChickenFeet
  • 2,653
  • 22
  • 26
pmm
  • 646
  • 9
  • 18
9

My function makes life easier.

function has_specchar($x,$excludes=array()){
    if (is_array($excludes)&&!empty($excludes)) {
        foreach ($excludes as $exclude) {
            $x=str_replace($exclude,'',$x);        
        }    
    }    
    if (preg_match('/[^a-z0-9 ]+/i',$x)) {
        return true;        
    }
    return false;
}

The second parameter ($excludes) may be passed with values you wish to ignore.

Usage

$string = 'testing_123';
if (has_specchar($string)) { 
    // special characters found
} 

$string = 'testing_123';
$excludes = array('_');
if (has_specchar($string,$excludes)) { } // false
David D
  • 1,269
  • 17
  • 22
1

For me, this works best:

$string = 'Test String';

$blacklistChars = '"%\'*;<>?^`{|}~/\\#=&';

$pattern = preg_quote($blacklistChars, '/');
if (preg_match('/[' . $pattern . ']/', $string)) {
   // string contains one or more of the characters in var $blacklistChars
}
codiga
  • 537
  • 5
  • 15
0

You can use preg_quote to escape charaters to use inside a regex expression:

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

http://php.net/manual/en/function.preg-quote.php

Petah
  • 45,477
  • 28
  • 157
  • 213
0

This works well for all PHP versions. The resultant is a bool and needs to be used accordingly.

To check id the string contains characters you can use this:

preg_match( '/[a-zA-Z]/', $string );

To check if a string contains numbers you can use this.

preg_match( '/\d/', $string );

Now to check if a string contains special characters, this one should be used.

preg_match('/[^a-zA-Z\d]/', $string);

Gene Z. Ragan
  • 2,643
  • 2
  • 31
  • 41
0

In case you want to match on special characters

preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $input)