简体   繁体   English

PHP-单独的大小写字符

[英]PHP - Separate upper and lower case characters

I want to separate upper or lower case characters. 我想分隔大写或小写字符。

In this text : "HELLO WORLD how are you" 在此文本中:“ HELLO WORLD,你好吗”

I want to have in the first variable : "HELLO WORLD" and "how are you" in the second. 我想在第一个变量中输入:第二个变量“ HELLO WORLD”和“你好吗”。

How can I do that with php ? 我如何用php做到这一点?

Thanks a lot 非常感谢

This is assuming that the first part of the string is uppercase and the rest is normal (eg mixed case). 假设字符串的第一部分是大写的,其余部分是正常的(例如,混合大小写)。 Like in the example you posted. 就像在您发布的示例中一样。

preg_match('/^([A-Z\s]+)(.*)$/', $string, $match);
$first_part = trim($match[1]);
$second_part = trim($match[2]);

If uppercase and mixed case words are mixed in the sentence, then I would do it as such (note, this replaces everything between words with single spaces). 如果句子中混用了大写和大小写混合的单词,那么我会这样做(注意,这会将单词之间的所有内容替换为单个空格)。

$words = preg_split('/[^\w]+/', $string);
$upper = '';
$lower = '';

foreach ($words as $word) {
    if ($word == strtoupper($word)) {
        $upper .= ' ' . $word;
    } else {
        $lower .= ' ' . $word;
    }
}

If your input string is always an UPPER CASE SEQUENCE followed by a lower case sequence , something like this might do it 如果您的输入字符串始终是UPPER CASE SEQUENCE后跟lower case sequence ,则可能会执行以下操作

if (preg_match('/^([A-Z\s]+)\s+([a-z\s+])$/', $str, $matches))
{
   $upper=$matches[1];
   $lower=$matches[2];

}

To walk through that regex 遍历该正则表达式

  • ^ anchors the match the to start of the string, so that we test the entire string ^将匹配项锚定到字符串的开头,以便我们测试整个字符串
  • ( opening parenthesis begins a group we can extract later as $matches[1] (开头的括号会开始一个组,稍后我们可以将其提取为$matches[1]
  • [AZ\\s] is a character class to match chars which are A through Z or whitespce [AZ\\s]是一个字符类,用于匹配从A到Z或whitespce的字符
  • + makes sure there is one or more such chars +确保有一个或多个这样的字符
  • ) closes the first group )关闭第一组
  • then we match a little more whitespace before looking for the lower case part in a similar fashion 然后我们再匹配更多的空格,然后再以类似的方式查找小写部分
  • $ anchors the match to the end of the string, so that we can be sure we've considered the entire string $将匹配项锚定到字符串的末尾,这样可以确保我们已经考虑了整个字符串

Just to use a different, non-regexp approach 只是使用另一种非正则表达式方法

$original = "HELLO WORLD how are you";

$upper = trim(implode(array_intersect(str_split($original),str_split(strtoupper($original)))));
$lower = trim(implode(array_intersect(str_split($original),str_split(strtolower($original)))));

echo $upper,'<br />';
echo $lower,'<br />';

Although regexp is best method for determine upper or lower, also you can use this function, for understand whether upper or lower of a character. 尽管regexp是确定大写或小写的最佳方法,但是您也可以使用此功能来了解字符的大写或小写。 If string that been sent as parameter includes completely upper, this function will return true value 如果作为参数发送的字符串包含完全上位,则此函数将返回真值

function is_upper($str)
{
$str2=strtoupper($str);
$$str="x";
if($$str==$$str2)
{

return true;

}
else
return false;
}

I know this is a bit late, but I stumbled across this question while looking for something else. 我知道这有点晚了,但是我在寻找其他问题时偶然发现了这个问题。 Anyway, preg_split works for this situation: 无论如何, preg_split适用于这种情况:

$original = "HELLO WORLD how are you";

$split = preg_split(
    '/([[:lower:]].*)/',
    $original,
    0,
    PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);

print_r($split);

Outputs: 输出:

Array
(
    [0] => HELLO WORLD 
    [1] => how are you
)

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

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