6 * @subpackage PluginsModifier
10 * Smarty capitalize modifier plugin
13 * Name: capitalize<br>
14 * Purpose: capitalize words in the string
16 * {@internal {$string|capitalize:true:true} is the fastest option for MBString enabled systems }}
18 * @param string $string string to capitalize
19 * @param boolean $uc_digits also capitalize "x123" to "X123"
20 * @param boolean $lc_rest capitalize first letters, lowercase all following letters "aAa" to "Aaa"
21 * @return string capitalized string
22 * @author Monte Ohrt <monte at ohrt dot com>
25 function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false)
27 if (Smarty::$_MBSTRING) {
29 // uppercase (including hyphenated words)
30 $upper_string = mb_convert_case( $string, MB_CASE_TITLE, Smarty::$_CHARSET );
32 // uppercase word breaks
33 $upper_string = preg_replace("!(^|[^\p{L}'])([\p{Ll}])!eS" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').mb_convert_case(stripslashes('\\2'),MB_CASE_UPPER, '" . addslashes(Smarty::$_CHARSET) . "')", $string);
35 // check uc_digits case
37 if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER, $string, $matches, PREG_OFFSET_CAPTURE)) {
38 foreach($matches[1] as $match) {
39 $upper_string = substr_replace($upper_string, mb_strtolower($match[0], Smarty::$_CHARSET), $match[1], strlen($match[0]));
43 $upper_string = preg_replace("!((^|\s)['\"])(\w)!e" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').mb_convert_case(stripslashes('\\3'),MB_CASE_UPPER, '" . addslashes(Smarty::$_CHARSET) . "')", $upper_string);
49 $string = strtolower($string);
51 // uppercase (including hyphenated words)
52 $upper_string = preg_replace("!(^|[^\p{L}'])([\p{Ll}])!eS" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').ucfirst(stripslashes('\\2'))", $string);
53 // check uc_digits case
55 if (preg_match_all("!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER, $string, $matches, PREG_OFFSET_CAPTURE)) {
56 foreach($matches[1] as $match) {
57 $upper_string = substr_replace($upper_string, strtolower($match[0]), $match[1], strlen($match[0]));
61 $upper_string = preg_replace("!((^|\s)['\"])(\w)!e" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').strtoupper(stripslashes('\\3'))", $upper_string);