1 | <?php |
||
21 | abstract class AbstractStrlen |
||
22 | { |
||
23 | /** |
||
24 | * |
||
25 | * Is the `mbstring` extension loaded? |
||
26 | * |
||
27 | * @return bool |
||
28 | * |
||
29 | */ |
||
30 | 75 | protected function mbstring() |
|
31 | { |
||
32 | 75 | return extension_loaded('mbstring'); |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * Is the `iconv` extension loaded? |
||
38 | * |
||
39 | * @return bool |
||
40 | * |
||
41 | */ |
||
42 | 119 | protected function iconv() |
|
46 | |||
47 | /** |
||
48 | * |
||
49 | * Proxy to `iconv_strlen()` or `mb_strlen()` when available; fall back to |
||
50 | * `utf8_decode()` and `strlen()` otherwise. |
||
51 | * |
||
52 | * @param string $str Return the number of characters in this string. |
||
53 | * |
||
54 | * @return int |
||
55 | * |
||
56 | */ |
||
57 | 121 | protected function strlen($str) |
|
58 | { |
||
59 | 121 | if ($this->iconv()) { |
|
60 | 117 | return $this->strlenIconv($str); |
|
61 | } |
||
62 | |||
63 | 4 | if ($this->mbstring()) { |
|
64 | 2 | return mb_strlen($str, 'UTF-8'); |
|
65 | } |
||
66 | |||
67 | 2 | return strlen(utf8_decode($str)); |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * |
||
72 | * Wrapper for `iconv_substr()` to throw an exception on malformed UTF-8. |
||
73 | * |
||
74 | * @param string $str The string to work with. |
||
75 | * |
||
76 | * @param int $start Start at this position. |
||
77 | * |
||
78 | * @param int $length End after this many characters. |
||
79 | * |
||
80 | * @return string |
||
81 | * |
||
82 | * @throws Exception\MalformedUtf8 |
||
83 | * |
||
84 | */ |
||
85 | 49 | protected function substrIconv($str,$start,$length) |
|
97 | |||
98 | /** |
||
99 | * |
||
100 | * Wrapper for `iconv_strlen()` to throw an exception on malformed UTF-8. |
||
101 | * |
||
102 | * @param string $str Return the number of characters in this string. |
||
103 | * |
||
104 | * @return int |
||
105 | * |
||
106 | * @throws Exception\MalformedUtf8 |
||
107 | * |
||
108 | */ |
||
109 | 117 | protected function strlenIconv($str) |
|
121 | |||
122 | /** |
||
123 | * |
||
124 | * Proxy to `iconv_substr()` or `mb_substr()` when the `mbstring` available; |
||
125 | * polyfill via `preg_split()` and `array_slice()` otherwise. |
||
126 | * |
||
127 | * @param string $str The string to work with. |
||
128 | * |
||
129 | * @param int $start Start at this position. |
||
130 | * |
||
131 | * @param int $length End after this many characters. |
||
132 | * |
||
133 | * @return string |
||
134 | * |
||
135 | */ |
||
136 | 53 | protected function substr($str, $start, $length = null) |
|
149 | |||
150 | /** |
||
151 | * |
||
152 | * Userland UTF-8-aware implementation of `str_pad()`. |
||
153 | * |
||
154 | * @param string $input The input string. |
||
155 | * |
||
156 | * @param int $pad_length If the value of pad_length is negative, less than, |
||
157 | * or equal to the length of the input string, no padding takes place. |
||
158 | * |
||
159 | * @param string $pad_str Pad with this string. The pad_string may be |
||
160 | * truncated if the required number of padding characters can't be evenly |
||
161 | * divided by the pad_string's length. |
||
162 | * |
||
163 | * @param int $pad_type Optional argument pad_type can be STR_PAD_RIGHT, |
||
164 | * STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is |
||
165 | * assumed to be STR_PAD_RIGHT. |
||
166 | * |
||
167 | * @return string |
||
168 | * |
||
169 | */ |
||
170 | 9 | protected function strpad($input, $pad_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT) |
|
207 | } |
||
208 |