Reputation: 3324
i use this code in php to detect if the string contains characters other than a-z,A-Z,0-9. I want to check if the string contains any spaces. What should I add to the pattern?
if (preg_match ('/[^a-zA-Z0-9]/i', $getmail)) {
// The string contains characters other than a-z and A-Z and 0-9
}
Upvotes: 2
Views: 8831
Reputation: 93177
You can try this
/[^a-z0-9\s]/i
As you used the i
modifier no need for A-Z
. The \s
part will mean spaces, but also tabs or any white space.
Upvotes: 4
Reputation: 382706
If you want to add space to your current pattern, you actually need to input space in your pattern:
/[^a-z0-9 ]/i
^
Notice the space there
Upvotes: 8
Reputation: 2592
if (preg_match('/[^a-z0-9 ]/i', $getmail)) // The string contains characters other than a-z (case insensitive), 0-9 and space
Note I removed A-Z from the character class too as you have case insensitivity turned on.
Upvotes: 2