how to replace all Uppercase letters with spacing?

php, regex

Solution

What about something like this :

$string = "MaryGoesToSchool";

$spaced = preg_replace('/([A-Z])/', ' $1', $string);
var_dump($spaced);

This :

- Matches the uppercase letters

- And replace each one of them by a space, and what was matched

Which gives this output :

string ' Mary Goes To School' (length=20)

And you can then use :

$trimmed = trim($spaced);
var_dump($trimmed);

To remove the space at the beginning, which gets you :

string 'Mary Goes To School' (length=19)

Problem

``` $string = "MaryGoesToSchool"; $expectedoutput = "Mary Goes To School"; ```

Original source