Search code examples
phpstringuppercaselowercasetitle-case

Add spaces between words in a camelCased string then uppercase the first word


I have two types of strings, hello and helloThere.

What I want is to change them so they read like: Hello and Hello There depending on the case.

What would be a good way of doing this?


Solution

  • you can use ucwords like everyone said... to add the space in helloThere you can do $with_space = preg_replace('/[A-Z]/'," $0",$string); then ucwords($with_space);