Stann0rz
Stann0rz

Reputation: 667

PHP - Regex for a string of special characters

Morning SO. I'm trying to determine whether or not a string contains a list of specific characters.

I know i should be using preg_match for this, but my regex knowledge is woeful and i have been unable to glean any information from other posts around this site. Since most of them just want to limit strings to a-z, A-Z and 0-9. But i do want some special characters to be allowed, for example: ! @ £ and others not in the below string.

Characters to be matched on: # $ % ^ & * ( ) + = - [ ] \ ' ; , . / { } | \ " : < > ? ~

private function containsIllegalChars($string)
{
    return preg_match([REGEX_STRING_HERE], $string);
}

I originally wrote the matching in Javascript, which just looped through each letter in the string and then looped through every character in another string until it found a match. Looking back, i can't believe i even attempted to use such an archaic method. With the advent of json (and a rewrite of the application!), i'm switching the match to php, to return an error message via json.

I was hoping a regex guru could assist with converting the above string to a regex string, but any feedback would be appreciated!

Upvotes: 11

Views: 56169

Answers (5)

NOTE: backslash is not working while answering to this query so Iam replaceing backslash symbol with backslash word.

Using backslash(\backslash) we can allow limited special characters. For example if you want to allow a-zA-Z0-9 (digits and numbers) you can write

reg_exp as:

$sampleString ="hello world! this is @2024";

We can filter out special characters using the following

$filteredText = preg_replace('/[^A-Za-z0-9]/', '', $string);

echo $filteredText;

//if you want to include space then use the following
$filteredText = preg_replace('/[^A-Za-z0-9 ]/', '', $string);

//if you want to allow any special character then place the special character after backslash(\) as follows

for only special charcters use:

($filteredText = preg_replace('/[^backslash-]/i', '', $string);)

including text and digits use:

($filteredText = preg_replace('/[^A-Za-z0-9 backslash-]/', '', $string);)

for multiple special charcters use:

($filteredText = preg_replace('/[^backslash-backslash,backslash!backslash@backslash#backslash$]/i', '', $string);)

including text and digits use:

 ($filteredText = preg_replace('/[^A-Za-z0-9 backslash-\,backslash!backslash@backslash#backslash$]/', '', $string);)

Upvotes: 0

Iwnnay
Iwnnay

Reputation: 2008

I think what you're looking for can be greatly simplified by including the characters that you want to allow like so:

preg_match('/[^\w!@£]/', $string)

Here's a quick breakdown of what's happening:

  • [^] = not included
  • \w = letters and numbers
  • ! @ £ = the list of characters you would also like to allow

Upvotes: 4

Arnold Daniels
Arnold Daniels

Reputation: 16553

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

Upvotes: 12

Dan Lugg
Dan Lugg

Reputation: 20592

$pattern = preg_quote('#$%^&*()+=-[]\';,./{}|\":<>?~', '#');

var_dump(preg_match("#[{$pattern}]#", 'hello world')); // false
var_dump(preg_match("#[{$pattern}]#", 'he||o wor|d')); // true
var_dump(preg_match("#[{$pattern}]#", '$uper duper')); // true

Likely, you can cache the $pattern, depending on your implementation.

(Though looking outside of regular expressions, you're best of with strpbrk as mentioned here too)

Upvotes: 3

Touki
Touki

Reputation: 7525

Regexp for a "list of disallowed character" is not mandatory.

You may have a look at strpbrk. It should do the job you need.

Here's an example of usage

$tests = array(
    "Hello I should be allowed",
    "Aw! I'm not allowed",
    "Geez [another] one",
    "=)",
    "<WH4T4NXSS474K>"
);
$illegal = "#$%^&*()+=-[]';,./{}|:<>?~";

foreach ($tests as $test) {
    echo $test;
    echo ' => ';
    echo (false === strpbrk($test, $illegal)) ? 'Allowed' : "Disallowed";
    echo PHP_EOL;
}

http://codepad.org/yaJJsOpT

Upvotes: 15

Related Questions