note 90521 modified in function.ucwords by danbrown

From: Date: Mon, 27 Apr 2009 15:38:00 +0000
Subject: note 90521 modified in function.ucwords by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153843@lists.php.net to get a copy of this message
I did the same thing as Catalin, but for French names. Here's what I'm doing : For each word (not considering the single-quote as a word boundary character) : - Put the word in lower case - If the word is "de", return, else, put the first letter in upper-case - See if the second character of the word is a single-quote - Yes ? Put the next char in upper case - And if the char before the single quote is D, put it back to lower case (-> d) This complies with the French rules for capitalization in names. Sample results : -d'Afoo Bar -de Foo Bar -O'Foo Bar -Foo'bar <?php function my_ucwords($s) { $s = preg_replace_callback("/(\b[\w|']+\b)/s", fixcase_callback, $s); return $s; } function fixcase_callback($word) { $word = $word[1]; $word = strtolower($word); if($word == "de") return $word; $word = ucfirst($word); if(substr($word,1,1) == "'") { if(substr($word,0,1) == "D") { $word = strtolower($word); } $next = substr($word,2,1); $next = strtoupper($next); $word = substr_replace($word, $next, 2, 1); } return $word; } ?> --was-- I did the same thing as Catalin, but for French names. Here's what I'm doing : For each word (not considering the single-quote as a word boundary character) : - Put the word in lower case - If the word is "de", return, else, put the first letter in upper-case - See if the second character of the word is a single-quote - Yes ? Put the next char in upper case - And if the char before the single quote is D, put it back to lower case (-> d) This complies with the French rules for capitalization in names. Sample results : -d'Afoo Bar -de Foo Bar -O'Foo Bar -Foo'bar function my_ucwords($s) { $s = preg_replace_callback("/(\b[\w|']+\b)/s", fixcase_callback, $s); return $s; } function fixcase_callback($word) { $word = $word[1]; $word = strtolower($word); if($word == "de") return $word; $word = ucfirst($word); if(substr($word,1,1) == "'") { if(substr($word,0,1) == "D") { $word = strtolower($word); } $next = substr($word,2,1); $next = strtoupper($next); $word = substr_replace($word, $next, 2, 1); } return $word; } http://php.net/manual/en/function.ucwords.php

« previous php.notes (#153843) next »