1 <?php
2 /**
3 * @package utf8
4 */
5
6 //---------------------------------------------------------------
7 /**
8 * UTF-8 aware alternative to ucwords
9 * Uppercase the first character of each word in a string
10 * Note: requires utf8_substr_replace and utf8_strtoupper
11 * @param string
12 * @return string with first char of each word uppercase
13 * @see http://www.php.net/ucwords
14 * @package utf8
15 */
16 function utf8_ucwords($str) {
17 // Note: [\x0c\x09\x0b\x0a\x0d\x20] matches;
18 // form feeds, horizontal tabs, vertical tabs, linefeeds and carriage returns
19 // This corresponds to the definition of a "word" defined at http://www.php.net/ucwords
20 $pattern = '/(^|([\x0c\x09\x0b\x0a\x0d\x20]+))([^\x0c\x09\x0b\x0a\x0d\x20]{1})[^\x0c\x09\x0b\x0a\x0d\x20]*/u';
21 return preg_replace_callback($pattern, 'utf8_ucwords_callback',$str);
22 }
23
24 //---------------------------------------------------------------
25 /**
26 * Callback function for preg_replace_callback call in utf8_ucwords
27 * You don't need to call this yourself
28 * @param array of matches corresponding to a single word
29 * @return string with first char of the word in uppercase
30 * @see utf8_ucwords
31 * @see utf8_strtoupper
32 * @package utf8
33 */
34 function utf8_ucwords_callback($matches) {
35 $leadingws = $matches[2];
36 $ucfirst = utf8_strtoupper($matches[3]);
37 $ucword = utf8_substr_replace(ltrim($matches[0]),$ucfirst,0,1);
38 return $leadingws . $ucword;
39 }
40
41