1

I am trying to write a simple program which takes every 4th letter (not character) in a string (not counting spaces) and changes the case to it's opposite (If it's in lower, change it to upper or vice versa).

What I have so far:

echo preg_replace_callback('/.{5}/', function ($matches){
            return ucfirst($matches[0]);   
     }, $strInput);

Sample input:

The sky is blue

Desired result:

The Sky iS bluE
#   ^4th ^8th ^12th
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
eksiekso
  • 11
  • 5

4 Answers4

2
$str = 'The sky is blue';
    $strArrWithSpace = str_split ($str);
    $strWithoutSpace = str_replace(" ", "", $str);
    $strArrWithoutSpace = str_split ($strWithoutSpace);
    $updatedStringWithoutSpace = '';
    $blankPositions = array();
    $j = 0;
    foreach ($strArrWithSpace as $key => $char) {
        if (empty(trim($char))) {
            $blankPositions[] = $key - $j;
            $j++;
        }
    }

    foreach ($strArrWithoutSpace as $key => $char) {
        if (($key +1) % 4 === 0) {
            $updatedStringWithoutSpace .= strtoupper($char);
        } else {
            $updatedStringWithoutSpace .= $char;
        }

    }

    $arrWithoutSpace = str_split($updatedStringWithoutSpace);
    $finalString = '';
    foreach ($arrWithoutSpace as $key => $char) {
        if (in_array($key, $blankPositions)) {
            $finalString .= ' ' . $char;
        } else {
            $finalString .= $char;
        }
    }

    echo $finalString;
Maulik Savaliya
  • 1,262
  • 7
  • 17
1

Try this:

$newStr = '';
foreach(str_split($str) as $index => $char) {
    $newStr .= ($index % 2) ? strtolower($char) : strtoupper($char);
}

it capitalize every 2nd character of string

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • 1
    Requirement is every 4th character without counting space – Saurabh Aug 03 '16 at 05:14
  • That seems to work for capitalizing every 2nd character like you said, but when I change it to 4 it is not giving me the correct output. It is giving me the output: "The Sky Is bLue" when I change ..."($index % 4)"... – eksiekso Aug 03 '16 at 05:21
  • This will only work if the length of words is always 4. Once you split and store it in a new variable, there is no way of tracking/counting the multiples of 4th character in the given string. Instead you are actually counting fourth character in every word. – Hari Harker Aug 03 '16 at 06:07
1
<?php
    $str = "The sky is blue";
    $str = str_split($str);
    $nth = 4; // the nth letter you want to replace
    $cnt = 0;
    for ($i = 0; $i < count($str); $i++) {
        if($str[$i]!=" " && $cnt!=$nth)
            $cnt++;
        if($cnt==$nth)
        {
            $cnt=0;
            $str[$i] = ctype_upper($str[$i])?strtolower($str[$i]):strtoupper($str[$i]);
        }
    }
    echo implode($str);
?>

This code satisfies all of your conditions.

Edit:

I would have used

$str = str_replace(" ","",$str);

to ignore the whitespaces in the string. But as you want them in the output as it is, so had to apply the above logic.

Jay Patel
  • 378
  • 6
  • 18
  • That works, thank you! One last thing. Any code I can add to this to get it to not count commas (so that it is only counting every nth letter and excluding symbols)? – eksiekso Aug 03 '16 at 06:09
  • you can place that in the if condition itself where whitespace is compared with the character. (Like: $str[$i]!=",") It will work. – Jay Patel Aug 03 '16 at 15:37
0

Implement any of the case-toggling techniques from Invert case of all letters in a string (uppercase to lowercase and lowercase to uppercase) after matching every 4th letter.

\K tells the tegex engine to forget the previously matched characters.

Code: (Demo)

$string = 'The sky is blue';
echo preg_replace_callback(
         '/(?:[^a-z]*\K[a-z]){4}/i',
         fn($m) => $m[0] ^ ' ',
         $string
     );

This answer is not crafted to be multibyte safe. Some multibyte characters do not have a toggle-able case letter inherently.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136