开发者

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 str开发者_JAVA技巧ing.

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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜