如何检查它上面的字符串在一个单词中是否有混合字母?



如何检查它上面的字符串是否在一个单词中混合了(拉丁语和西里尔文(符号? 例如:

$str = 'This is test string'; //ok
$str = 'This is тест string'; //ok
$str = 'This is тестstring'; // <-- fail, how to detect this?

更多示例:

$str = 'This is тест_123 string'; //ok
$str = 'This is {тест}_string'; //fail
$str = 'Абвгabcd'; //fail
$str = 'Абвг_abcd'; //fail
$str = 'Абвг abcd'; //ok
$str = 'This sentence has русское word'; //ok
$str = 'This has splittedкириллицаletters word'; //fail

对于没有匹配项,这将返回0,对于匹配项,这将返回1。 您需要添加任何不允许进入[a-z]的特殊字符,例如[a-z}{]

$result = preg_match('/([a-z]p{Cyrillic})|(p{Cyrillic}[a-z])/iu', $str, $matches);

要获取单词,请将$matches作为第三个参数传递,它将填充匹配项。 要获取多个匹配项,请执行以下操作:

preg_match_all('/([a-z]p{Cyrillic})|(p{Cyrillic}[a-z])/iu', $str, $matches);

反其道而行之,找到好词:

preg_match_all('/([a-z]s+p{Cyrillic})|(p{Cyrillic}s+[a-z])/iu', $str, $matches);

找到解决方案,它通过了所有测试

$result = preg_match_all('/S*[а-яА-Я]S*[a-zA-Z]S*|S*[a-zA-Z]S*[а-яА-Я]S*/', $str, $matches);

相关内容