#native_company# #native_desc#
#native_cta#

Customizable ucwords function

By Sjoerd
on March 2, 2002

Version: 1.1

Type: Function

Category: Other

License: GNU General Public License

Description: function wil convert the first letter of each word in a string to uppercase.

For example “THE MUSIC MAN WITH THE LATIN COMBO” will be converted to “The Music Man With The Latin Combo”.

However, if an array with exception words is given, for instance containing “the”, “with” and “his”, the result will be “The Music Man with his Latin Combo. (note: the first occurence of “The” produces a capital “T” because “The” is the first word of the string.)

Please look at the examples below the function code for a better understanding.

<?
/*
function wil convert the first letter of each word in a string to uppercase. For 
example "THE MUSIC MAN WITH THE LATIN COMBO" will be converted to "The Music Man 
With The Latin Combo". However, if an array with exception words is given, for 
instance containing "the", "with" and "his", the result will be "The Music Man with 
his Latin Combo. (note: the first occurence of "The" produces a capital "T" 
because "The" is the first word of the string.) Please look at the examples below 
the function code for a better understanding.
*/

function ucwords_cus($string, $array="empty"){
  
  if (!is_array($array) && $array != "empty") $array = "off";

  if ($array == "empty") {  //default array with exception words:
        $array = array();
	$array[0] = "the";
        $array[1] = "with";
	$array[2] = "by";
	$array[3] = "among";
	$array[4] = "and";
	$array[5] = "or";
  }
  
  $words = explode(" ", $string);
  $tel = 0;
  $add = "";

  foreach ($words as $word){
    if ($tel != 0) $add = " ";

	//check if first letter of word is uppercase:
	$firstletter = substr($word,0,1);
	$firstletter_lowercase = strtolower($firstletter);
	
	if ($firstletter != $firstletter_lowercase) {
	  $first = "uppercase";
	} else $first = "lowercase";

	
    //check if second letter of word is uppercase:
        $secondletter = substr($word,1,1);
	$secondletter_lowercase = strtolower($firstletter);
	
	if ($secondletter != $secondletter_lowercase) {
          $second = "uppercase";
	} else $second = "lowercase";

    //check if first two letters both are uppercase:
	if ($first == "uppercase" && $second == "uppercase"){
      
	  
	  //check if the word is one of the exception words:
	  $res = false;
	  if ($array != "off" && $tel != 0){  //only used excepted words filtering if no 0 was passed as second argument:
	    foreach ($array as $resword){
          if (strtolower($word) == strtolower($resword)) $res = true;
	    }
	  }

	  if ($res){  //an exception word: convert to lowercase only:
	    $word = strtolower($word);
	  } else { //first convert entire word to lowercase, then capitalize first letter with ucfirst():	
	    $word = ucfirst(strtolower($word));
	  }

    }

	$filtered .= $add.$word;
	$tel = 1;
  }
	return $filtered;
}


//examples:

$string = "THE GEORGE BAKER TRIO AND HIS BACKING VOCALS IN THE SOCIAL CLUB";

echo ucwords_cus($string); //use the default array with exception words.

?><p><?

	$testarray = array();
	$testarray[0] = "and";
        $testarray[1] = "his";
	$testarray[2] = "in";
	$testarray[3] = "the";
	
echo ucwords_cus($string, $testarray); //don't use the default array, be a passed one.

?><p><?

echo ucwords_cus($string, "off");  //exception words functionality is off.

?>