This is a rather custom little function, but still may prove of use. It’s a PHP function that properly capitalizes a name (first and last), and ensures the other characters are lowercased. I have a vague memory of basing this on someone else’s code… but it’s long since gone from my mind.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /*--------------------------------------------------------------------------*\ Function: format_name Purpose: takes a name as a parameter and attempts to correctly format it for upper- and lower-case. \*--------------------------------------------------------------------------*/ function format_name($name) { $name = strtolower(stripslashes($name)); // uppercase any multi-barrelled names $names_array = explode('-', $name); for ($i=0; $i<count($names_array); $i++) { // for names like McDonald or O'Connor if (strncmp($names_array[$i],'mc',2) == 0 || ereg('^[oO]\'[a-zA-Z]', $names_array[$i])) $names_array[$i][2] = strtoupper($names_array[$i][2]); // always set the first letter to uppercase, no matter what $names_array[$i] = ucfirst($names_array[$i]); } // Piece the names back together $name = implode('-',$names_array); // return upper-casing on all missed (but required) elements return ucwords($name); } |
Recent Comments