检查一个单词是否有多个大写字母,只更改只有一个大写字母(并且是第一个字母)的单词

Check if a word has multiple uppercases letters and only change the words who have One and only uppercase (and be the first letter )

到目前为止我的代码:

$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>";
}

结果:

Herman
Archer
Lives
In
New
York

预期输出:

Herman Archer lives in new york

让我们以简单的方式来做。让我们循环$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;
        }
    }
}

如果您不仅使用英语,您可能想切换到 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

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

<?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);

您可以将第一个字符组合为 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;

与此同时,我发现这可以通过更简单的方式完成

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). " ";
                    }
                }