Monday, November 7, 2016

PHP: Replace space in front of capitals in a string (Regex)

I have a number of strings which contain words which are bunched together and I need to seperate them up.

For example ThisWasCool - This Was Cool MyHomeIsHere - My Home Is Here

$String = 'TheCambodia';
$Words = preg_replace('/(?<!\ )[A-Z]/', ' $0', $String);
echo $Words;

Output: The Cambodia