tags:

views:

61

answers:

3

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
}
+1  A: 
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.

chigley
+1  A: 

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
Sarfraz
@Sarfraz nice touch, LMAO
Phill Pafford
@Phill Pafford: Thank you :)
Sarfraz
+2  A: 

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.

Colin Hebert