Mega Code Archive

 
Categories / Php / Strings
 

A function to take a string and converts the first letter of all words to upper case

<?php function SM_ucfirst_all ($text_string, $firstonly = x) { //optional flag to convert all words to lowercase first if($firstonly != x) $text_string = strtolower($text_string); //use explode to create an array with one word in each item $text_array = explode(" ", $text_string) ; //loop through the array while(list($key,$val) = each($text_array)) { $val = ucfirst ($val); $new_text_string .= $val.' ' ; } return $new_text_string; }//end //function SM_ucfirst_all(string str [, string str]) //This funtion takes a string and converts the first letter of all words //to upper case // //e.g. //$text = 'one TWO three four'; //print SM_ucfirst_all($text); //prints 'One TWO Three Four' // //Using the optional flag to convert all words to lower case and then //uppercase first letters // //print SM_ucfirst_all($text,1); // //prints 'One Two Three Four' ?>