JoshFinnie
JoshFinnie

Reputation: 4911

Alternating Capital Letters in Array Using PHP

I have an alphanumeric string like below,

$string_1 = "a4nas60dj71wiena15sdl1131kg12b"

and would like to change it to something like below,

$string_2 = "a4NaS60dJ71wIeNa15Sdl1131Kg12B"

How would I go about doing this? I have tried the below code, but it doesn't work.

$lenght = strlen($string_1);

for ( $i = 0; $i <= $length - 1; $i += 1) {

    if ( $i % 2) {

        $string_2[$i]=strtoupper($string_1[$i]);

    }

    else {

        $string_2[$i]=$string_1[$i];

    }

}

echo $string_2;

The above code prints out "Array" so something is definitely not working.

Upvotes: 0

Views: 2118

Answers (3)

James Skidmore
James Skidmore

Reputation: 50318

You should be good to go with this function. Just call alternate('string here'); and it will work perfectly.

This works with hyphens, white space, periods, etc. included in the string.

function alternate($string1)
{
    $do_caps = false;
    $string2 = '';
    for ($i = 0; $i < strlen($string1); $i++)
    {
      $char = substr($string1, $i, 1);
      if (stripos('abcdefghijklmnopqrstuvwxyz', $char) !== false)
      {
            if ($do_caps)
            {
                $char = strtoupper($char);
                $do_caps = false;
            }
            else
            {
                $do_caps = true;
            }
      }
      $string2 .= $char;
    }
    return $string2;
}

Upvotes: 0

cletus
cletus

Reputation: 625237

By the way, you have a slight error in your capitalized string:

$string_1: a4nas60dj71wiena15sdl1131kg12b
$string_2: a4NaS60dJ71wIeNa15Sdl1131Kg12B
                               ^ should be capital so out of sync for rest of string

I'll give you two ways of doing it:

<?php
header('Content-Type: text/plain');

$string_1 = "a4nas60dj71wiena15sdl1131kg12b";
$string_2 = "a4NaS60dJ71wIeNa15Sdl1131Kg12B";

$letter_count = 0;
$result = '';
for ($i=0; $i<strlen($string_1); $i++) {
    if (!preg_match('![a-zA-Z]!', $string_1[$i])) {
        $result .= $string_1[$i];
    } else if ($letter_count++ & 1) {
        $result .= strtoupper($string_1[$i]);
    } else {
        $result .= $string_1[$i];
    }
}

$result2 = preg_replace_callback('!([a-zA-Z]\d*)([a-zA-Z])!', 'convert_to_upper', $string_1);

function convert_to_upper($matches) {
    return strtolower($matches[1]) . strtoupper($matches[2]);
}

echo "$string_1\n";
echo "$string_2\n";
echo "$result\n";
echo "$result2\n";
?>

Note: The above makes several assumptions:

  1. Characters other than numbers and letters can be in the string;
  2. You want to alternate case regardless of the original (eg "ASDF" becomes "aSdF");
  3. You're capitalizing every second letter, not every second lowercase letter.

The above can be altered if these assumptions are incorrect.

Upvotes: 1

Jonathan Fingland
Jonathan Fingland

Reputation: 57177

Strings are not arrays, and you're declaring $string_2 as an array...

Still, your code won't work quite as expected, since, you're alternating case of every other letter, while ignoring numbers

try the following:

function altCaps($str) {
    $lower = true;
    $str2 = "";
    for ($i=0;$len=strlen($str);$i<$len;++$i) {
        $char = substr($str,$i,1);
        if (is_numeric($char)) {
            $str2 .= $char;
        } else {
            if ($lower) {
                $str2 .= strtolower($char);
            } else {
                $str2 .= strtolower($char);
            }
            $lower = !$lower;
        }
    }
    return $str2;
}

Upvotes: 0

Related Questions