Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何检查一个单词中有混合字母的字符串?_Php - Fatal编程技术网

Php 如何检查一个单词中有混合字母的字符串?

Php 如何检查一个单词中有混合字母的字符串?,php,Php,如何检查一个单词中有混合(拉丁和西里尔)符号的字符串? 例如: $str = 'This is test string'; //ok $str = 'This is тест string'; //ok $str = 'This is тестstring'; // <-- fail, how to detect this? 如果不匹配,则返回0,如果匹配,则返回1。您需要在[a-z]中添加任何不允许的特殊字符,例如[a-z}{]: $result = preg_match('/([a-

如何检查一个单词中有混合(拉丁和西里尔)符号的字符串? 例如:

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

如果不匹配,则返回
0
,如果匹配,则返回
1
。您需要在
[a-z]
中添加任何不允许的特殊字符,例如
[a-z}{]

$result = preg_match('/([a-z]\p{Cyrillic})|(\p{Cyrillic}[a-z])/iu', $str, $matches);
要获取单词pass
$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);

你做了哪些尝试?为什么
tu123
ok?为什么
tu-string
而不是
tu-string
通过?这听起来更像是某种习惯(似乎是随机的)拼写检查比字符检查更重要,因为它们包含完全相同的字符,减去额外的空格。@Abracadver
СС砦峎123
是可以的,因为在这种情况下,符号不是混合的(拉丁语和西里尔语),只有西里尔语、数字和字母specials@MagnusEriksson如果我理解的话,我添加了更多的示例。
$result = preg_match_all('/\S*[а-яА-Я]\S*[a-zA-Z]\S*|\S*[a-zA-Z]\S*[а-яА-Я]\S*/', $str, $matches);