简体   繁体   English

在大写字母和小写字母之间拆分单词

[英]Split words between UPPER CASE and lower case

If anyone could help me this would be awesome! 如果有人可以帮助我,那就太好了!

I have this: 我有这个:

TYLLON kevin -convert-> familyname: TYLLON ; TYLLON kevin >姓氏: TYLLON ; prename: kevin 姓氏: kevin

VAN AZERTY bert -convert-> familyname: VAN AZERTY ; VAN AZERTY bert -convert->姓氏: VAN AZERTY prename: bert 姓氏: bert

YAHOO BE AWESOME rabbit -convert-> familyname: YAHOO BE AWESOME ; YAHOO BE AWESOME rabbit Rabbit-convert->家族名称: YAHOO BE AWESOME ; prename: rabbit 姓氏: rabbit

Maybe regex, anyone? 也许正则表达式,有人吗?

I believe shortest possible answer is by using preg_split here using look-arounds: 我相信最短的答案是在此处使用环顾preg_split

Use this regex for splitting: 使用此正则表达式进行拆分:

/(?<=\p{Lu})\h+(?=\p{Ll})/u

Which matches 1 or more horizontal space that is preceded by a uppercase unicode letter and followed by a lowercase unicode letter. 匹配1个或多个水平空间,水平空间前跟一个大写Unicode字母,再跟一个小写Unicode字母。

PS: This solution is unicode compatible. PS:此解决方案与unicode兼容。

Examples: 例子:

print_r(preg_split('/(?<=\p{Lu})\h+(?=\p{Ll})/u', 'YAHOO BE AWESOME rabbit'));
Array
(
    [0] => YAHOO BE AWESOME
    [1] => rabbit
)

print_r(preg_split('/(?<=\p{Lu})\h+(?=\p{Ll})/u', 'VAN AZERTY bert'));
Array
(
    [0] => VAN AZERTY
    [1] => bert
)

print_r(preg_split('/(?<=\p{Lu})\h+(?=\p{Ll})/u', 'TYLLON kevin'));
Array
(
    [0] => TYLLON
    [1] => kevin
)

You could loop through your name strings using the php function ctype_upper : 您可以使用php函数ctype_upper遍历您的名称字符串:

Checks if all of the characters in the provided string, text, are uppercase characters. 检查提供的字符串,文本中的所有字符是否均为大写字符。

You would first have to explode() your names and put them in an array. 首先,您必须explode()您的名称并将其放入数组中。 Once you have them in an array you can check for upper case / lower case… 将它们放入数组后,您可以检查大写/小写…

<?php
$name_array = array('VAN', 'AZERTY', 'bert');
foreach ($name_array as $testcase) {
    if (ctype_upper($testcase)) {
        echo "The string $testcase is upper case.";
    } else {
        echo "The string $testcase is lower case / mixed case.";
    }
}
?>

Try this solution. 试试这个解决方案。

<?php
   preg_match_all('/\b([A-Z]+)\b/', $fullname, $upper);
   preg_match_all('/\b([a-z]+)\b/', $fullname, $lower);
   $familyname = implode(' ', $upper[0]);
   $prename = implode(' ', $lower[0]);
   echo $prename.' '.$familyname;
?>

I found the answer in combination of multiple answers: 我找到了结合多个答案的答案:

preg_match_all('/\b([A-Z]+)\b/', 'BLA BOEM BABA Kevin', $matches);

RESULT: BLA BOEM BABA 结果:BLA BOEM BABA

preg_replace('/\b([A-Z]+)\b/', '', 'VAN MELKEBEKE BLA BOEM BABA Kevin');

RESULT: Kevin 结果:凯文

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

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