Question: There is a method to know which characters does not match a preg\_match function?
For example:

```
preg_match('/^[a-z]*$/i', 'Hello World!');

```

Is there some function to know the incorrect char, in this case spance and "!"?

---

Thanks for your replies, but the problem in your examples is you don't indicate the begin and the end of the string. Your examples works with string contained in another one and not with the string that is exactly like I defined in the pattern.

For example, if I had to validate the italian fiscal code of a subject, composed by a string formatted like this:

```
XXX XXX YY X YY X YYY X (X = letter, Y = number - without spaces)

```

which pattern is:

```
'/^[A-Z]{6}[0-9]{2}[A-Z]{1}[0-9]{2}[A-Z]{1}[0-9]{3}[A-Z]{1}$/i'

```

I must validate the string that match exactly what I defined in the pattern.

If I use your code and I wrong 1 (only 1) character, the whole string was returned as error.
<http://eval.in/9178>

The problem of the reverse pattern occurs in a complex pattern, where are inserted the AND or the OR.

What I want to know is why the preg\_match fails and not only if it fails or not.

Answer: 
Have you tried something like this?

```
$nonMatchingCharacters = preg_replace('/[a-z]/', '', $wholeString);

```

That should strip out the 'legal' characters, leaving only the ones that you want to mention in your validation error message.

You could also do other treatments like...

```
$nonMatchingCharactersArray = array_unique(explode('', $nonMatchingCharacters));

```

...if you want an array of unique, non-matching characters, and not just a string with bits stripped out of it.
Just remove everything that matches with preg\_replace, then split into an array what remains.

```
<?php

$str = preg_replace('/([0-9]{2}[a-z]*)/i', '', '03Hello 02World!');

$characters = str_split($str);

var_dump($characters);

```

<http://eval.in/9152>