Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
    $string = (string) $string;
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_callback(
34
                "!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER,
35
                'smarty_mod_cap_mbconvert_cb',
36
                $string
37
            );
38
        }
39
        // check uc_digits case
40
        if (!$uc_digits) {
41
            if (preg_match_all(
42
                "!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER,
43
                $string,
44
                $matches,
45
                PREG_OFFSET_CAPTURE
46
            )
47
            ) {
48
                foreach ($matches[ 1 ] as $match) {
49
                    $upper_string =
50
                        substr_replace(
51
                            $upper_string,
52
                            mb_strtolower($match[ 0 ], Smarty::$_CHARSET),
53
                            $match[ 1 ],
54
                            strlen($match[ 0 ])
55
                        );
56
                }
57
            }
58
        }
59
        $upper_string =
60
            preg_replace_callback(
61
                "!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER,
62
                'smarty_mod_cap_mbconvert2_cb',
63
                $upper_string
64
            );
65
        return $upper_string;
66
    }
67
    // lowercase first
68
    if ($lc_rest) {
69
        $string = strtolower($string);
70
    }
71
    // uppercase (including hyphenated words)
72
    $upper_string =
73
        preg_replace_callback(
74
            "!(^|[^\p{L}'])([\p{Ll}])!S" . Smarty::$_UTF8_MODIFIER,
75
            'smarty_mod_cap_ucfirst_cb',
76
            $string
77
        );
78
    // check uc_digits case
79
    if (!$uc_digits) {
80
        if (preg_match_all(
81
            "!\b([\p{L}]*[\p{N}]+[\p{L}]*)\b!" . Smarty::$_UTF8_MODIFIER,
82
            $string,
83
            $matches,
84
            PREG_OFFSET_CAPTURE
85
        )
86
        ) {
87
            foreach ($matches[ 1 ] as $match) {
88
                $upper_string =
89
                    substr_replace($upper_string, strtolower($match[ 0 ]), $match[ 1 ], strlen($match[ 0 ]));
90
            }
91
        }
92
    }
93
    $upper_string = preg_replace_callback(
94
        "!((^|\s)['\"])(\w)!" . Smarty::$_UTF8_MODIFIER,
95
        'smarty_mod_cap_ucfirst2_cb',
96
        $upper_string
97
    );
98
    return $upper_string;
99
}
100
 
101
/**
102
 *
103
 * Bug: create_function() use exhausts memory when used in long loops
104
 * Fix: use declared functions for callbacks instead of using create_function()
105
 * Note: This can be fixed using anonymous functions instead, but that requires PHP >= 5.3
106
 *
107
 * @author Kyle Renfrow
108
 */
109
/**
110
 * @param $matches
111
 *
112
 * @return string
113
 */
114
function smarty_mod_cap_mbconvert_cb($matches)
115
{
116
    return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 2 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
117
}
118
 
119
/**
120
 * @param $matches
121
 *
122
 * @return string
123
 */
124
function smarty_mod_cap_mbconvert2_cb($matches)
125
{
126
    return stripslashes($matches[ 1 ]) . mb_convert_case(stripslashes($matches[ 3 ]), MB_CASE_UPPER, Smarty::$_CHARSET);
127
}
128
 
129
/**
130
 * @param $matches
131
 *
132
 * @return string
133
 */
134
function smarty_mod_cap_ucfirst_cb($matches)
135
{
136
    return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 2 ]));
137
}
138
 
139
/**
140
 * @param $matches
141
 *
142
 * @return string
143
 */
144
function smarty_mod_cap_ucfirst2_cb($matches)
145
{
146
    return stripslashes($matches[ 1 ]) . ucfirst(stripslashes($matches[ 3 ]));
147
}