简体   繁体   中英

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 aa 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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM