Keyword parsing: separating words by capital letters in PHP

I was trying to create a simple routine for separating the keywords within a domain name based on user input of capitalized words. For example, the user might input:

This should be parsed into the term “my domain name” based on the capital letters signifying the first letters of each word.

As usual, I Googled to see if someone had already done this, no use reinventing the wheel. However, I did not really find anything good, so I decided to tackle the problem myself. Here’s the code I came up with:


$ctype_test = preg_replace(’/[^a-zA-Z]/’, ”, $domain_name);

// the above line gets rid of everything except letters
if (!ctype_lower($ctype_test) and !ctype_upper($ctype_test)) {

// check whether there are BOTH upper and lowercase letters in the domain name

// otherwise there is no sense in continuing with the routine
for ($i=0;$i<strlen($domain_name); $i++){     // loop as many times as the string is long
if (ctype_upper($string_split[$i]) AND $i>0){  // if there is an uppercase letter
$string_split[$i]=” “.$string_split[$i];         // then add a space in front of the letter

foreach($string_split as $value){     // loop through all the letters
$term=$term.$value;                // add each letter to the TERM to make up the parsed term

$term=strtolower($term); // finally, make the term all lowercase


Maybe there’s a better way to do it, but I though this was nice & simple.