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

- 4,738
- 23
- 27
- 42

- 3,264
- 10
- 53
- 79
-
3Define "special characters"... – Alex Howansky Oct 14 '10 at 21:05
-
[^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`] – stefanosn Oct 14 '10 at 21:12
-
2Why do you want to use `preg_match()`? If all you're doing is checking for the existence of these characters, why not use `strpos()`? – staticsan Oct 14 '10 at 22:33
-
If your input is ASCII characters then you could just check by the character integer ranges. – Yzmir Ramirez Mar 10 '12 at 01:59
7 Answers
[\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
}

- 3,466
- 2
- 30
- 52

- 346
- 2
- 3
-
Thank you for your answer but i need only specific characters like ^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬` – stefanosn Oct 14 '10 at 21:40
-
3Normally 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
-
@d.raev that is expected for it to include # as a non-word character – Jacob Smith Mar 30 '19 at 05:50
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.

- 2,653
- 22
- 26

- 646
- 9
- 18
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

- 1,269
- 17
- 22
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
}

- 537
- 5
- 15
You can use preg_quote
to escape charaters to use inside a regex expression:
preg_match('/' . preg_quote("[^'£$%^&*()}{@:'#~?><>,;@|\-=-_+-¬`]", '/') . '/', $string);

- 45,477
- 28
- 157
- 213
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);

- 2,643
- 2
- 31
- 41

- 18
- 7
In case you want to match on special characters
preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $input)

- 1
- 2