views:

44

answers:

2

I am using a regular expression to see if there is a single non-latin character within a string.

$latin_check = '/[\x{0030}-\x{007f}]/u'; //This is looking for only latin characters


if(preg_match($latin_check, $_POST['full_name'])) { 
    $error = true;        
}

This should be checking to see if there is at least one character present that is not a a latin character. If it does not find at least a single non-latin character, then it should set $error to true.

I think my logic may be wrong. How can I find a single occurence of a non-latin character using regular expressions in php?

+2  A: 

The following should work:

if (preg_match('/[^\x30-\x7F]/', $_POST['full_name']) > 0)
{
    $error = true;
}

You may want to use x20 instead of x30 as your lower boundary if you want to accept space and the following ASCII symbols: !"#$%&'()*+,-./.

Alix Axel
+2  A: 

Your regular expression (as far as I can tell) will match any character between hex codes \x0030 and \x007f, which is the opposite of what you want. If you're looking to find characters that aren't in that range (i.e. - non Latin characters), I think you'll want to use a negated character class:

$latin_check = '/[^\x{0030}-\x{007f}]/u';

The '^' metacharacter in a regular expression character class basically means "any characters that are not within this character class".

eldarerathis