@graup stuff |
A loose collection of interesting stuff I come across. Programming, Traveling, Studying Also check out my personal homepage |
We needed a way to normalize user names. Cuz you know, people keep entering their names in an ugly way, which annoys the admins’ eyes.
As it turns out, there’s actually a function built into PHP which does that. I then added a revert for words like “von” or “of”.
All put together (with credits to the at psychoticneurotic dot com):
function name($string) {
$exceptions = array('von','de','to', 'a', 'der', 'the', 'of', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X');
$string = mb_convert_case($string, MB_CASE_TITLE, "UTF-8");
/* revert exceptions */
$words = explode(' ',$string);
$words2 = array();
foreach($words AS $word) {
if (in_array(strtolower($word), $exceptions)) {
$word = strtolower($word);
}
array_push($words2, $word);
}
$string = implode(' ',$words2);
return $string;
}