Home > Blockchain >  Check if a word has multiple uppercases letters and only change the words who have One and only uppe
Check if a word has multiple uppercases letters and only change the words who have One and only uppe

Time:10-21

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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;

CodePudding user response:

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