Subversion Repositories ALCASAR

Rev

Details | Last modification | View Log

Rev Author Line No. Line
2809 rexy 1
<?php
2
/**
3
 * Smarty plugin
4
 *
5
 * @package    Smarty
6
 * @subpackage PluginsModifier
7
 */
8
/**
9
 * Smarty capitalize modifier plugin
10
 * Type:     modifier
11
 * Name:     capitalize
12
 * Purpose:  capitalize words in the string
13
 * {@internal {$string|capitalize:true:true} is the fastest option for MBString enabled systems }}
14
 *
15
 * @param string  $string    string to capitalize
16
 * @param boolean $uc_digits also capitalize "x123" to "X123"
17
 * @param boolean $lc_rest   capitalize first letters, lowercase all following letters "aAa" to "Aaa"
18
 *
19
 * @return string capitalized string
20
 * @author Monte Ohrt <monte at ohrt dot com>
21
 * @author Rodney Rehm
22
 */
23
function smarty_modifier_capitalize($string, $uc_digits = false, $lc_rest = false)
24
{
25
    if (Smarty::$_MBSTRING) {
26
        if ($lc_rest) {
27
            // uppercase (including hyphenated words)
28
            $upper_string = mb_convert_case($string, MB_CASE_TITLE, Smarty::$_CHARSET);
29
        } else {
30
            // uppercase word breaks
31
            $upper_string = preg_replace_callback(
32
                "!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER,
33
                'smarty_mod_cap_mbconvert_cb',
34
                $string
35
            );
36
        }
37
        // check uc_digits case
38
        if (!$uc_digits) {
39
            if (preg_match_all(
40
                "!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER,
41
                $string,
42
                $matches,
43
                PREG_OFFSET_CAPTURE
44
            )
45
            ) {
46
                foreach ($matches[ 1 ] as $match) {
47
                    $upper_string =
48
                        substr_replace(
49
                            $upper_string,
50
                            mb_strtolower($match[ 0 ], Smarty::$_CHARSET),
51
                            $match[ 1 ],
52
                            strlen($match[ 0 ])
53
                        );
54
                }
55
            }
56
        }
57
        $upper_string =
58
            preg_replace_callback(
59
                "!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER,
60
                'smarty_mod_cap_mbconvert2_cb',
61
                $upper_string
62
            );
63
        return $upper_string;
64
    }
65
    // lowercase first
66
    if ($lc_rest) {
67
        $string = strtolower($string);
68
    }
69
    // uppercase (including hyphenated words)
70
    $upper_string =
71
        preg_replace_callback(
72
            "!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER,
73
            'smarty_mod_cap_ucfirst_cb',
74
            $string
75
        );
76
    // check uc_digits case
77
    if (!$uc_digits) {
78
        if (preg_match_all(
79
            "!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER,
80
            $string,
81
            $matches,
82
            PREG_OFFSET_CAPTURE
83
        )
84
        ) {
85
            foreach ($matches[ 1 ] as $match) {
86
                $upper_string =
87
                    substr_replace($upper_string, strtolower($match[ 0 ]), $match[ 1 ], strlen($match[ 0 ]));
88
            }
89
        }
90
    }
91
    $upper_string = preg_replace_callback(
92
        "!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER,
93
        'smarty_mod_cap_ucfirst2_cb',
94
        $upper_string
95
    );
96
    return $upper_string;
97
}
98
 
99
/**
100
 *
101
 * Bug: create_function() use exhausts memory when used in long loops
102
 * Fix: use declared functions for callbacks instead of using create_function()
103
 * Note: This can be fixed using anonymous functions instead, but that requires PHP >= 5.3
104
 *
105
 * @author Kyle Renfrow
106
 */
107
/**
108
 * @param $matches
109
 *
110
 * @return string
111
 */
112
function smarty_mod_cap_mbconvert_cb($matches)
113
{
114
    return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 2 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
115
}
116
 
117
/**
118
 * @param $matches
119
 *
120
 * @return string
121
 */
122
function smarty_mod_cap_mbconvert2_cb($matches)
123
{
124
    return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 3 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
125
}
126
 
127
/**
128
 * @param $matches
129
 *
130
 * @return string
131
 */
132
function smarty_mod_cap_ucfirst_cb($matches)
133
{
134
    return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 2 ]));
135
}
136
 
137
/**
138
 * @param $matches
139
 *
140
 * @return string
141
 */
142
function smarty_mod_cap_ucfirst2_cb($matches)
143
{
144
    return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 3 ]));
145
}