Specific uppercase editing

57 views Asked by At

I need to set uppercase letters to every word in the text starting from the second one. These words always have an underscore at the beginning and the text does not have a fixed length. I have no clue on how to do this.

Example:

input: my_little_green_house

result: my_Little_Green_House

Any help will be greatly appreciated.

1

There are 1 answers

1
Tony Dinh On BEST ANSWER

Try this:

$output = str_replace(" ", "_",lcfirst(ucwords(strtolower(str_replace("_", " ", $input))))

Explain:

$output = str_replace(" ", "_", // 5. Convert all spaces to underscores again.
          lcfirst( // 4. Lowercase the first character.
              ucwords( // 3. Uppercase first letter of every words
                  strtolower( // 2. Convert to lowercase
                      str_replace("_", " ", $input) // 1. Convert all underscores to spaces
                  )
              )
          )

Guys, I intent to make a fiddle but http://phpfiddle.org/ is down, anyone have an alternative php fiddle over here?