source: trunk/include/smarty/libs/plugins/modifier.capitalize.php @ 23384

Last change on this file since 23384 was 23384, checked in by rvelices, 11 years ago

smarty 3 - first pass for tests

  • Property svn:eol-style set to LF
File size: 2.7 KB
Line 
1<?php
2/**
3 * Smarty plugin
4 *
5 * @package Smarty
6 * @subpackage PluginsModifier
7 */
8
9/**
10 * Smarty capitalize modifier plugin
11 *
12 * Type:     modifier<br>
13 * Name:     capitalize<br>
14 * Purpose:  capitalize words in the string
15 *
16 * {@internal {$string|capitalize:true:true} is the fastest option for MBString enabled systems }}
17 *
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>
23 * @author Rodney Rehm
24 */
25function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false)
26{
27    if (Smarty::$_MBSTRING) {
28        if ($lc_rest) {
29            // uppercase (including hyphenated words)
30            $upper_string = mb_convert_case( $string, MB_CASE_TITLE, Smarty::$_CHARSET );
31        } else {
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);
34        }
35        // check uc_digits case
36        if (!$uc_digits) {
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]));
40                }
41            } 
42        }
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);
44        return $upper_string;
45    }
46   
47    // lowercase first
48    if ($lc_rest) {
49        $string = strtolower($string);
50    }
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
54    if (!$uc_digits) {
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]));
58            }
59        } 
60    }
61    $upper_string = preg_replace("!((^|\s)['\"])(\w)!e" . Smarty::$_UTF8_MODIFIER, "stripslashes('\\1').strtoupper(stripslashes('\\3'))", $upper_string);
62    return $upper_string;
63} 
64
65?>
Note: See TracBrowser for help on using the repository browser.