Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 217343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:38:55+00:00 2026-05-11T18:38:55+00:00

I have an alphanumeric string like below, $string_1 = a4nas60dj71wiena15sdl1131kg12b and would like to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:38:55+00:00Added an answer on May 11, 2026 at 6:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string that I would like to validate a certain pattern for:
I have a string formatted like this: item_questions_attributes_abc123_id I'm specifically trying to get the
I have some string data with alphanumeric value. like us01name, phc01name and other i.e
I have one string, that looks like this: a[abcdefghi,2,3,jklmnopqr] The beginning a is fixed
I have a string with 16 alphanumeric characters, e.g. F4194E7CC775F003. I'd like to format
I have a string which could be like this: $searchterm = The quick brown
For eg., i have alphanumeric string 'ABCDEF 0 0.450' and i need to get
I am using Python 2.7 . I have an alphanumeric string, on which I
I have a question about removing non-alphanumeric characters from a list in R. I
I have a page consisting of many elements with alphanumeric ids such as this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.