简体   繁体   中英

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

My code so far:

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

Result:

Herman
Archer
Lives
In
New
York

Expected output:

Herman Archer lives in new york

Let's do it in a simple manner. Let's loop $oldWords , compare the strings from the second character to the end with their lower-case version and replace if the result is different.

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

If you are using not only English language, you might want to switch to 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

If you want to use the counter approach you could use something as the following

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

you can combine ctype_upper for first character and ctype_lower for the rest

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

Meanwhile I've found out that this can be done in an easier way

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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