1
|
|
|
<?php |
2
|
|
|
namespace morphos; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Multibyte string helper |
6
|
|
|
*/ |
7
|
|
|
class S |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Sets encoding for using in morphos/* functions. |
11
|
|
|
* @param string $encoding |
12
|
|
|
* @return bool |
13
|
|
|
*/ |
14
|
|
|
public static function setEncoding($encoding) |
15
|
|
|
{ |
16
|
|
|
if (function_exists('mb_internal_encoding')) { |
17
|
|
|
mb_internal_encoding($encoding); |
18
|
|
|
} elseif (function_exists('iconv_set_encoding')) { |
19
|
|
|
iconv_set_encoding('internal_encoding', $encoding); |
20
|
|
|
} else { |
21
|
|
|
return false; |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Calculates count of characters in string. |
27
|
|
|
* @param $string |
28
|
|
|
* @return bool|int |
29
|
|
|
*/ |
30
|
34 |
|
public static function length($string) |
31
|
|
|
{ |
32
|
34 |
|
if (function_exists('mb_strlen')) { |
33
|
34 |
|
return mb_strlen($string); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (function_exists('iconv_strlen')) { |
37
|
|
|
return iconv_strlen($string); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Slices string like python. |
45
|
|
|
* @param string $string |
46
|
|
|
* @param int $start |
47
|
|
|
* @param int|null $end |
48
|
|
|
* @return bool|string |
49
|
|
|
*/ |
50
|
1392 |
|
public static function slice($string, $start, $end = null) |
51
|
|
|
{ |
52
|
1392 |
|
if ($end != null) { |
|
|
|
|
53
|
580 |
|
$end -= $start; |
54
|
|
|
} |
55
|
|
|
|
56
|
1392 |
|
if (function_exists('mb_substr')) { |
57
|
1392 |
|
return $end === null ? mb_substr($string, $start) : mb_substr($string, $start, $end); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (function_exists('iconv_substr')) { |
61
|
|
|
return $end === null ? iconv_substr($string, $start) : iconv_substr($string, $start, $end); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Lower case. |
69
|
|
|
* @param $string |
70
|
|
|
* @return bool|string |
71
|
|
|
*/ |
72
|
1647 |
|
public static function lower($string) |
73
|
|
|
{ |
74
|
1647 |
|
if (function_exists('mb_strtolower')) { |
75
|
1647 |
|
return mb_strtolower($string); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Upper case. |
83
|
|
|
* @param $string |
84
|
|
|
* @return bool|string |
85
|
|
|
*/ |
86
|
201 |
|
public static function upper($string) |
87
|
|
|
{ |
88
|
201 |
|
if (function_exists('mb_strtoupper')) { |
89
|
201 |
|
return mb_strtoupper($string); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Name case. (ex: Thomas, Lewis) |
97
|
|
|
* @param $string |
98
|
|
|
* @return bool|string |
99
|
|
|
*/ |
100
|
199 |
|
public static function name($string) |
101
|
|
|
{ |
102
|
199 |
|
if (function_exists('mb_strtoupper')) { |
103
|
199 |
|
return implode('-', array_map(function ($word) { |
104
|
199 |
|
return self::upper(self::slice($word, 0, 1)).self::lower(self::slice($word, 1)); |
105
|
199 |
|
}, explode('-', $string))); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* multiple substr_count(). |
113
|
|
|
* @param $string |
114
|
|
|
* @param array $chars |
115
|
|
|
* @return bool|int |
116
|
|
|
*/ |
117
|
|
|
public static function countChars($string, array $chars) |
118
|
|
|
{ |
119
|
|
|
if (function_exists('mb_split')) { |
120
|
|
|
return count(mb_split('('.implode('|', $chars).')', $string)) - 1; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $string |
128
|
|
|
* @param string $char |
129
|
|
|
* @return bool|string |
130
|
|
|
*/ |
131
|
7 |
|
public static function findFirstPosition($string, $char) |
132
|
|
|
{ |
133
|
7 |
|
if (function_exists('mb_strpos')) { |
134
|
7 |
|
return mb_strpos($string, $char, 0); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return strpos($string, $char); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $string |
142
|
|
|
* @param string $char |
143
|
|
|
* @return bool|string |
144
|
|
|
*/ |
145
|
5 |
|
public static function findLastPosition($string, $char) |
146
|
|
|
{ |
147
|
5 |
|
if (function_exists('mb_strrpos')) { |
148
|
5 |
|
return mb_strrpos($string, $char, 0); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return strrpos($string, $char); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param $string |
156
|
|
|
* @param array $chars |
157
|
|
|
* @return bool|string |
158
|
|
|
*/ |
159
|
43 |
|
public static function findLastPositionForOneOfChars($string, array $chars) |
160
|
|
|
{ |
161
|
43 |
|
if (function_exists('mb_strrpos')) { |
162
|
43 |
|
$last_position = false; |
163
|
43 |
|
foreach ($chars as $char) { |
164
|
43 |
|
if (($pos = mb_strrpos($string, $char)) !== false) { |
165
|
43 |
|
if ($pos > $last_position) { |
166
|
43 |
|
$last_position = $pos; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
43 |
|
if ($last_position !== false) { |
171
|
43 |
|
return mb_substr($string, $last_position); |
172
|
|
|
} |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $string |
181
|
|
|
* @param $substring |
182
|
|
|
* @param bool $caseSensetive |
183
|
|
|
* @param int $startOffset |
184
|
|
|
* @return string|false |
185
|
|
|
*/ |
186
|
1 |
|
public static function indexOf($string, $substring, $caseSensetive = false, $startOffset = 0) |
187
|
|
|
{ |
188
|
1 |
|
if (function_exists('mb_stripos')) { |
189
|
1 |
|
return $caseSensetive ? mb_strpos($string, $substring, $startOffset) : mb_stripos($string, $substring, $startOffset); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|