简体   繁体   English

如何使用正则表达式查找非拉丁字符的单次出现?

[英]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. 如果找不到至少一个非拉丁字符,则应将$ error设置为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? 如何使用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. 您的正则表达式(据我所知)将匹配十六进制代码\\ x0030和\\ x007f之间的任何字符,这与您想要的相反。 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: !"#$%&'()*+,-./ . 如果要接受空格和以下ASCII符号,则可能希望使用x20而不是x30作为下边界: !"#$%&'()*+,-./

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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