-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinStringStatic.php
More file actions
407 lines (378 loc) · 14.5 KB
/
BinStringStatic.php
File metadata and controls
407 lines (378 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<?php
/**
* If you use mbstring.func_overload, or the server you are running on has it enabled, you are in trouble.
* This class will help overcome that, by provide alternative functions to work around it.
*
* see readme.markdown for further details.
*
* @author A. Grandt <php@grandt.com>
* @copyright 2014 A. Grandt
* @license GNU LGPL 2.1
*/
namespace com\grandt;
class BinStringStatic {
const VERSION = 1.00;
/**
* mbstring.func_overload has an undocumented feature, to retain access to the original function.
* As it is undocumented, it is uncertain if it'll remain, therefore it's being made an optional.
*
* @var boolean
*/
public static $USE_MB_ORIG = false;
/**
* Initialize the vars that'll allow us to override mbstring.func_overload, if needed.
*
* @author A. Grandt <php@grandt.com>
*
* @return bool true if mbstring.func_overload is enabled.
*/
public static function isMBMailOverloaded() {
static $has_mb_mail_overload = null;
if ($has_mb_mail_overload === null) {
$has_mbstring = extension_loaded('mbstring');
$has_mb_shadow = (int) ini_get('mbstring.func_overload');
$has_mb_mail_overload = $has_mbstring && ($has_mb_shadow & 1);
}
return $has_mb_mail_overload;
}
/**
* Initialize the vars that'll allow us to override mbstring.func_overload, if needed.
*
* @author A. Grandt <php@grandt.com>
*
* @return bool true if mbstring.func_overload is enabled.
*/
public static function isMBStringOverloaded() {
static $has_mb_overload = null;
if ($has_mb_overload === null) {
$has_mbstring = extension_loaded('mbstring');
$has_mb_shadow = (int) ini_get('mbstring.func_overload');
$has_mb_overload = $has_mbstring && ($has_mb_shadow & 2);
}
return $has_mb_overload;
}
public static function getPHPVersionId() {
if (!defined('PHP_VERSION_ID')) {
$version = explode('.', PHP_VERSION);
define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}
return PHP_VERSION_ID;
}
/**
* Initialize the vars that'll allow us to override mbstring.func_overload, if needed.
*
* @author A. Grandt <php@grandt.com>
*
* @return bool true if mbstring.func_overload is enabled.
*/
public static function isMBRegexOverloaded() {
static $has_mb_regex_overload = null;
if ($has_mb_regex_overload === null) {
$has_mbstring = extension_loaded('mbstring');
$has_mb_shadow = (int) ini_get('mbstring.func_overload');
$has_mb_regex_overload = $has_mbstring && ($has_mb_shadow & 4);
}
return $has_mb_regex_overload;
}
/**
* @link http://php.net/manual/en/function.mail.php
*/
public static function _mail($to, $subject, $message, $additional_headers = null, $additional_parameters = null) {
if (self::isMBMailOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_mail($to, $subject, $message, $additional_headers, $additional_parameters);
}
$lang = mb_language(); // get current language
mb_language("en"); // Force encoding to iso8859-1
$rv = mb_send_mail($to, $subject, $message, $additional_headers, $additional_parameters);
mb_language($lang);
return $rv;
} else {
return mail($to, $subject, $message, $additional_headers, $additional_parameters);
}
}
/**
* @link http://php.net/manual/en/function.strlen.php
*/
public static function _strlen($string) {
if (self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strlen($string);
}
return mb_strlen($string, 'latin1');
} else {
return strlen($string);
}
}
/**
* @link http://php.net/manual/en/function.strpos.php
*/
public static function _strpos($haystack, $needle, $offset = 0) {
if (self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strpos($haystack, $needle, $offset);
}
return mb_strpos($haystack, $needle, $offset, 'latin1');
} else {
return strpos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.strrpos.php
*/
public static function _strrpos($haystack, $needle, $offset = 0) {
if (self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strrpos($haystack, $needle, $offset);
}
return mb_strrpos($haystack, $needle, $offset, 'latin1');
} else {
return strrpos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.stripos.php
*/
public static function _stripos($haystack, $needle, $offset = 0) {
if (self::getPHPVersionId() >= 50200 && self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_stripos($haystack, $needle, $offset);
}
return mb_stripos($haystack, $needle, $offset, 'latin1');
} else {
return stripos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.strripos.php
*/
public static function _strripos($haystack, $needle, $offset = 0) {
if (self::getPHPVersionId() >= 50200 && self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strripos($haystack, $needle, $offset);
}
return mb_strripos($haystack, $needle, $offset, 'latin1');
} else {
return strripos($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.strstr.php
*/
public static function _strstr($haystack, $needle, $before_needle = false) {
if (self::getPHPVersionId() >= 50200 && self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strstr($haystack, $needle, $before_needle);
}
return mb_strstr($haystack, $needle, $before_needle, 'latin1');
} else {
return strstr($haystack, $needle, $before_needle);
}
}
/**
* @link http://php.net/manual/en/function.stristr.php
*/
public static function _stristr($haystack, $needle, $before_needle = false) {
if (self::getPHPVersionId() >= 50200 && self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_stristr($haystack, $needle, $before_needle);
}
return mb_stristr($haystack, $needle, $before_needle, 'latin1');
} else {
return stristr($haystack, $needle, $before_needle);
}
}
/**
* @link http://php.net/manual/en/function.strrchr.php
*/
public static function _strrchr($haystack, $needle) {
if (self::getPHPVersionId() >= 50200 && self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strrchr($haystack, $needle);
}
return mb_strrchr($haystack, $needle, 'latin1');
} else {
return strrchr($haystack, $needle);
}
}
/**
* @link http://php.net/manual/en/function.substr.php
*/
public static function _substr($string, $start, $length = null) {
if (self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
if (func_num_args() == 2) { // Kludgry hack, as PHP substr is lobotomized.
return mb_orig_substr($string, $start);
}
return mb_orig_substr($string, $start, $length);
}
if (func_num_args() == 2) { // Kludgry hack, as mb_substr is lobotomized, AND broken.
return mb_substr($string, $start, mb_strlen($string, 'latin1'), 'latin1');
}
return mb_substr($string, $start, $length, 'latin1');
} else {
if (func_num_args() == 2) { // Kludgry hack, as PHP substr is lobotomized.
return substr($string, $start);
}
return substr($string, $start, $length);
}
}
/**
* @link http://php.net/manual/en/function.strtolower.php
*/
public static function _strtolower($string) {
if (self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strtolower($string);
}
return mb_strtolower($string, 'latin1');
} else {
return strtolower($string);
}
}
/**
* @link http://php.net/manual/en/function.strtoupper.php
*/
public static function _strtoupper($string) {
if (self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_strtoupper($string);
}
return mb_strtoupper($string, 'latin1');
} else {
return strtoupper($string);
}
}
/**
* @link http://php.net/manual/en/function.substr_count.php
*/
public static function _substr_count($haystack, $needle, $offset = 0, $length = null) {
if (self::isMBStringOverloaded()) {
if (self::$USE_MB_ORIG) {
if (func_num_args() > 3) { // Kludgry hack, as PHP substr_count is lobotomized.
return mb_orig_substr_count($haystack, $needle, $offset, $length);
}
return mb_orig_substr_count($haystack, $needle, $offset);
}
return mb_substr_count($haystack, $needle, 'latin1');
} else {
if (func_num_args() > 3) { // Kludgry hack, as PHP substr_count is lobotomized.
return substr_count($haystack, $needle, $offset, $length);
}
return substr_count($haystack, $needle, $offset);
}
}
/**
* @link http://php.net/manual/en/function.ereg.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public static function _ereg($pattern, $string, array &$regs) {
if (self::isMBRegexOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_ereg($pattern, $string, $regs);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_ereg($pattern, $string, $regs);
mb_regex_encoding($enc);
return $rv;
} else {
return ereg($pattern, $string, $regs);
}
}
/**
* @link http://php.net/manual/en/function.eregi.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public static function _eregi($pattern, $string, array &$regs) {
if (self::isMBRegexOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_eregi($pattern, $string, $regs);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_eregi($pattern, $string, $regs);
mb_regex_encoding($enc);
return $rv;
} else {
return eregi($pattern, $string, $regs);
}
}
/**
* @link http://php.net/manual/en/function.ereg_replace.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public static function _ereg_replace($pattern, $replacement, $string, $mb_specific_option = "msr") {
if (self::isMBRegexOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_ereg_replace($pattern, $replacement, $string);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_ereg_replace($pattern, $replacement, $string, $mb_specific_option);
mb_regex_encoding($enc);
return $rv;
} else {
return ereg_replace($pattern, $replacement, $string);
}
}
/**
* @link http://php.net/manual/en/function.eregi_replace.php
* @deprecated ereg_* functions are deprecated in PHP 5.3 onwards, use the PCRE preg_* instead.
*/
public static function _eregi_replace($pattern, $replacement, $string, $mb_specific_option = "msri") {
if (self::isMBRegexOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_eregi_replace($pattern, $replacement, $string);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_eregi_replace($pattern, $replacement, $string, $mb_specific_option);
mb_regex_encoding($enc);
return $rv;
} else {
return eregi_replace($pattern, $replacement, $string);
}
}
/**
* @link http://php.net/manual/en/function.split.php
* @deprecated Split is deprecated in PHP 5.3 onwards, use preg_split instead. It'll bypass mb_* anyway.
*/
public static function _split($pattern, $string, $limit = -1) {
if (self::isMBRegexOverloaded()) {
if (self::$USE_MB_ORIG) {
return mb_orig_split($pattern, $string, $limit);
}
$enc = mb_regex_encoding(); // get current encoding
mb_regex_encoding("latin1"); // Force encoding to iso8859-1
$rv = mb_split($pattern, $string, $limit);
mb_regex_encoding($enc);
return $rv;
} else {
return split($pattern, $string, $limit);
}
}
/**
* Checks if the $haystack starts with the text in the $needle.
* Case sensitive.
* @param string $haystack
* @param string $needle
*
* @return bool
*/
public static function startsWith($haystack, $needle) {
return $needle === '' || self::_strpos($haystack, $needle) === 0;
}
/**
* Checks if the $haystack ends with the text in the $needle.
* Case sensitive.
*
* @param string $haystack
* @param string $needle
*
* @return bool
*/
public static function endsWith($haystack, $needle) {
return $needle === '' || self::_substr($haystack, -self::_strlen($needle)) === $needle;
}
}