zeckdude
zeckdude

Reputation: 16163

How can I find a single occurrence of a non-latin character using regular expressions?

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?

Upvotes: 2

Views: 1790

Answers (2)

Alix Axel
Alix Axel

Reputation: 154553

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: !"#$%&'()*+,-./.

Upvotes: 2

eldarerathis
eldarerathis

Reputation: 36213

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".

Upvotes: 3

Related Questions