【问题标题】:Check if a word has multiple uppercases letters and only change the words who have One and only uppercase (and be the first letter )检查一个单词是否有多个大写字母,并且只更改只有一个大写的单词(并且是第一个字母)
【发布时间】:2021-12-07 02:59:53
【问题描述】:

到目前为止我的代码:

$text = 'Herman Archer LIVEs in neW YORK';
$oldWords = explode(' ', $text);

$newWords = array();

$counter = 0;

foreach ($oldWords as $word) {
    for($k=0;$k<strlen($word);$k++)
        $counter = 0;
        if ($word[k] == strtoupper($word[$k]))
            $counter=$counter+1;
        if($counter>1)
                  $word = strtolower($word);
        if($counter == 1)
                $word = ucfirst(strtolower($word));
           else $word = strtolower($word);

echo $word."<br>";
}

结果:

赫尔曼
弓箭手
生活


约克

预期输出:

赫尔曼·阿彻住在纽约

【问题讨论】:

  • 注意:您应该添加一些花括号{},或者注意缩进。在这里,很容易看出if 语句在第二个循环中...
  • 另外,在$word[k] 中,k 是一个未定义的常量,而不是$k
  • 目前,if-statements 您的for-loop 之后,不在其中(因为您仍然缺少{ })。这意味着$word[k](已经指出是错字)只会检查每个单词的最后一个字符。当前在您的for-loop 中发生的唯一一件事是您在每次迭代中都设置$counter = 0,仅此而已。我假设这不是本意?
  • @Baracuda078 使用正则表达式进行字符串操作太过分了
  • @DarkBee 我上次检查时,正则表达式仅用于字符串操作

标签: php uppercase lowercase


【解决方案1】:

让我们以一种简单的方式来做。让我们循环$oldWords,比较从第二个字符到结尾的字符串与它们的小写版本,如果结果不同则替换。

for ($index = 0; $index < count($oldWords); $index++) {
    //Skip one-lettered words, such as a or A
    if (strlen($oldWords[$index]) > 1) {
        $lower = strtolower($oldWords[$index]);
        if (substr($oldWords[$index], 1) !== substr($lower, 1)) {
            $oldWords[$index] = $lower;
        }
    }
}

【讨论】:

  • 我喜欢你的方法。我会研究它。谢谢
  • @ZackFair 确定。我的示例代码未经测试,所以如果它不起作用,很可能是我打错了。在这种情况下,请让我知道这个问题,我会调查它。编码愉快!
【解决方案2】:

如果您不仅使用英语,则可能需要切换到mb_strtolower

<?php

$text = 'Herman Archer LIVEs in neW YORK';
function normalizeText($text)
{
    $words = explode(" ", $text);
    $normalizedWords = array_map(function ($word) {
        $loweredWord = strtolower($word);
       
        if (ucfirst($loweredWord) === $word) {
            return $word;
        }

        return $loweredWord;
    }, $words);

    return join(" ", $normalizedWords);
}

echo normalizeText($text) . PHP_EOL; // Herman Archer lives in new york

【讨论】:

  • 哦,我没有为此而战。谢谢
【解决方案3】:

如果您想使用计数器方法,您可以使用以下内容

<?php
 
$text = 'Herman Archer LIVEs in A neW YORK';
$words = explode(' ', $text);
 
foreach($words as &$word) {
    $counter = 0;
    for($i = 1; $i <= strlen($word);$i++) {
        if (strtoupper($word[$i]) == $word[$i]) $counter++;
        if ($counter == 2) break;
    }
    if ($counter == 2) $word = strtolower($word);
}
echo implode(' ', $words);

【讨论】:

  • 感谢您提供此解决方案。它真的帮助了我
  • 不使用 implode 可以解决这个问题吗?
【解决方案4】:

您可以将 ctype_upper 用于第一个字符,将 ctype_lower 用于其余字符

$text = 'Herman Archer LIVEs in neW YORK';
$oldWords = explode(' ', $text);
$newWords = '';

foreach ($oldWords as $word) {
    if(ctype_upper($word[0])&&ctype_lower(substr($word,1))){
        $newWords .= $word.' ';
    }else{
        $newWords .= strtolower($word).' ';
    }
}

echo $newWords;

【讨论】:

    【解决方案5】:

    同时我发现这可以用更简单的方式完成

    if(isset($_POST["sumbit"])){
                $string = $_POST["string"];
                if(!empty($string)){
                    $word = explode (" ",$string);
                    foreach($words as $word){
                        //cut the first letter.
                        //check caselower.
                        //if not, attach the letter back and turn all lowercase.
                        //if yes, attach the letter back and leave it . 
                        $wordCut = substr($word,1);
                        if(ctype_lower($wordCut)){
                            echo $word." ";
                        } else {
                                echo strtolower($word). " ";
                        }
                    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-09
      • 2020-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      相关资源
      最近更新 更多