1
|
|
|
<?php defined('SYSPATH') or die('No direct access allowed.'); |
2
|
|
|
/** |
3
|
|
|
* A port of phputf8 to a unified file/class. Checks PHP status to ensure that |
4
|
|
|
* UTF-8 support is available and normalize global variables to UTF-8. It also |
5
|
|
|
* provides multi-byte aware replacement string functions. |
6
|
|
|
* |
7
|
|
|
* This file is licensed differently from the rest of Kohana. As a port of |
8
|
|
|
* phputf8, which is LGPL software, this file is released under the LGPL. |
9
|
|
|
* |
10
|
|
|
* PCRE needs to be compiled with UTF-8 support (--enable-utf8). |
11
|
|
|
* Support for Unicode properties is highly recommended (--enable-unicode-properties). |
12
|
|
|
* @see http://php.net/manual/reference.pcre.pattern.modifiers.php |
13
|
|
|
* |
14
|
|
|
* UTF-8 conversion will be much more reliable if the iconv extension is loaded. |
15
|
|
|
* @see http://php.net/iconv |
16
|
|
|
* |
17
|
|
|
* The mbstring extension is highly recommended, but must not be overloading |
18
|
|
|
* string functions. |
19
|
|
|
* @see http://php.net/mbstring |
20
|
|
|
* |
21
|
|
|
* $Id: utf8.php 3769 2008-12-15 00:48:56Z zombor $ |
22
|
|
|
* |
23
|
|
|
* @package Core |
24
|
|
|
* @author Kohana Team |
25
|
|
|
* @copyright (c) 2007 Kohana Team |
26
|
|
|
* @copyright (c) 2005 Harry Fuecks |
27
|
|
|
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
if (! preg_match('/^.$/u', 'ñ')) { |
31
|
|
|
trigger_error( |
32
|
|
|
'<a href="http://php.net/pcre">PCRE</a> has not been compiled with UTF-8 support. '. |
33
|
|
|
'See <a href="http://php.net/manual/reference.pcre.pattern.modifiers.php">PCRE Pattern Modifiers</a> '. |
34
|
|
|
'for more information. This application cannot be run without UTF-8 support.', |
35
|
|
|
E_USER_ERROR |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if (! extension_loaded('iconv')) { |
40
|
|
|
trigger_error( |
41
|
|
|
'The <a href="http://php.net/iconv">iconv</a> extension is not loaded. '. |
42
|
|
|
'Without iconv, strings cannot be properly translated to UTF-8 from user input. '. |
43
|
|
|
'This application cannot be run without UTF-8 support.', |
44
|
|
|
E_USER_ERROR |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if (extension_loaded('mbstring') and (ini_get('mbstring.func_overload') & MB_OVERLOAD_STRING)) { |
49
|
|
|
trigger_error( |
50
|
|
|
'The <a href="http://php.net/mbstring">mbstring</a> extension is overloading PHP\'s native string functions. '. |
51
|
|
|
'Disable this by setting mbstring.func_overload to 0, 1, 4 or 5 in php.ini or a .htaccess file.'. |
52
|
|
|
'This application cannot be run without UTF-8 support.', |
53
|
|
|
E_USER_ERROR |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
// Check PCRE support for Unicode properties such as \p and \X. |
58
|
|
|
$ER = error_reporting(0); |
59
|
|
|
define('PCRE_UNICODE_PROPERTIES', (bool) preg_match('/^\pL$/u', 'ñ')); |
60
|
|
|
error_reporting($ER); |
61
|
|
|
|
62
|
|
|
// SERVER_UTF8 ? use mb_* functions : use non-native functions |
63
|
|
|
if (extension_loaded('mbstring')) { |
64
|
|
|
mb_internal_encoding('UTF-8'); |
65
|
|
|
define('SERVER_UTF8', true); |
66
|
|
|
} else { |
67
|
|
|
define('SERVER_UTF8', false); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// Convert all global variables to UTF-8. |
71
|
|
|
$_GET = utf8::clean($_GET); |
72
|
|
|
$_POST = utf8::clean($_POST); |
73
|
|
|
$_COOKIE = utf8::clean($_COOKIE); |
74
|
|
|
$_SERVER = utf8::clean($_SERVER); |
75
|
|
|
|
76
|
|
|
if (PHP_SAPI == 'cli') { |
77
|
|
|
// Convert command line arguments |
78
|
|
|
$_SERVER['argv'] = utf8::clean($_SERVER['argv']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
final class utf8 |
82
|
|
|
{ |
83
|
|
|
|
84
|
|
|
// Called methods |
85
|
|
|
public static $called = array(); |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Recursively cleans arrays, objects, and strings. Removes ASCII control |
89
|
|
|
* codes and converts to UTF-8 while silently discarding incompatible |
90
|
|
|
* UTF-8 characters. |
91
|
|
|
* |
92
|
|
|
* @param string string to clean |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public static function clean($str) |
96
|
|
|
{ |
97
|
|
|
if (is_array($str) or is_object($str)) { |
98
|
|
|
foreach ($str as $key => $val) { |
99
|
|
|
// Recursion! |
100
|
|
|
$str[self::clean($key)] = self::clean($val); |
101
|
|
|
} |
102
|
|
|
} elseif (is_string($str) and $str !== '') { |
103
|
|
|
// Remove control characters |
104
|
|
|
$str = self::strip_ascii_ctrl($str); |
105
|
|
|
|
106
|
|
|
if (! self::is_ascii($str)) { |
107
|
|
|
// Disable notices |
108
|
|
|
$ER = error_reporting(~E_NOTICE); |
|
|
|
|
109
|
|
|
|
110
|
|
|
// iconv is expensive, so it is only used when needed |
111
|
|
|
$str = iconv('UTF-8', 'UTF-8//IGNORE', $str); |
112
|
|
|
|
113
|
|
|
// Turn notices back on |
114
|
|
|
error_reporting($ER); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $str; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Tests whether a string contains only 7bit ASCII bytes. This is used to |
123
|
|
|
* determine when to use native functions or UTF-8 functions. |
124
|
|
|
* |
125
|
|
|
* @param string string to check |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public static function is_ascii($str) |
129
|
|
|
{ |
130
|
|
|
return ! preg_match('/[^\x00-\x7F]/S', $str); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Strips out device control codes in the ASCII range. |
135
|
|
|
* |
136
|
|
|
* @param string string to clean |
137
|
|
|
* @param string $str |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public static function strip_ascii_ctrl($str) |
141
|
|
|
{ |
142
|
|
|
return preg_replace('/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S', '', $str); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Strips out all non-7bit ASCII bytes. |
147
|
|
|
* |
148
|
|
|
* @param string string to clean |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public static function strip_non_ascii($str) |
152
|
|
|
{ |
153
|
|
|
return preg_replace('/[^\x00-\x7F]+/S', '', $str); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Replaces special/accented UTF-8 characters by ASCII-7 'equivalents'. |
158
|
|
|
* |
159
|
|
|
* @author Andreas Gohr <[email protected]> |
160
|
|
|
* |
161
|
|
|
* @param string string to transliterate |
162
|
|
|
* @param integer -1 lowercase only, +1 uppercase only, 0 both cases |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
public static function transliterate_to_ascii($str, $case = 0) |
166
|
|
|
{ |
167
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
168
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
169
|
|
|
|
170
|
|
|
// Function has been called |
171
|
|
|
self::$called[__FUNCTION__] = true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return _transliterate_to_ascii($str, $case); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Returns the length of the given string. |
179
|
|
|
* @see http://php.net/strlen |
180
|
|
|
* |
181
|
|
|
* @param string string being measured for length |
182
|
|
|
* @return integer |
183
|
|
|
*/ |
184
|
|
|
public static function strlen($str) |
185
|
|
|
{ |
186
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
187
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
188
|
|
|
|
189
|
|
|
// Function has been called |
190
|
|
|
self::$called[__FUNCTION__] = true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return _strlen($str); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Finds position of first occurrence of a UTF-8 string. |
198
|
|
|
* @see http://php.net/strlen |
199
|
|
|
* |
200
|
|
|
* @author Harry Fuecks <[email protected]> |
201
|
|
|
* |
202
|
|
|
* @param string haystack |
203
|
|
|
* @param string needle |
204
|
|
|
* @param integer offset from which character in haystack to start searching |
205
|
|
|
* @param string $str |
206
|
|
|
* @return integer position of needle |
207
|
|
|
* @return boolean FALSE if the needle is not found |
|
|
|
|
208
|
|
|
*/ |
209
|
|
|
public static function strpos($str, $search, $offset = 0) |
210
|
|
|
{ |
211
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
212
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
213
|
|
|
|
214
|
|
|
// Function has been called |
215
|
|
|
self::$called[__FUNCTION__] = true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return _strpos($str, $search, $offset); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Finds position of last occurrence of a char in a UTF-8 string. |
223
|
|
|
* @see http://php.net/strrpos |
224
|
|
|
* |
225
|
|
|
* @author Harry Fuecks <[email protected]> |
226
|
|
|
* |
227
|
|
|
* @param string haystack |
228
|
|
|
* @param string needle |
229
|
|
|
* @param integer offset from which character in haystack to start searching |
230
|
|
|
* @param string $str |
231
|
|
|
* @return integer position of needle |
232
|
|
|
* @return boolean FALSE if the needle is not found |
|
|
|
|
233
|
|
|
*/ |
234
|
|
|
public static function strrpos($str, $search, $offset = 0) |
235
|
|
|
{ |
236
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
237
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
238
|
|
|
|
239
|
|
|
// Function has been called |
240
|
|
|
self::$called[__FUNCTION__] = true; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return _strrpos($str, $search, $offset); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Returns part of a UTF-8 string. |
248
|
|
|
* @see http://php.net/substr |
249
|
|
|
* |
250
|
|
|
* @author Chris Smith <[email protected]> |
251
|
|
|
* |
252
|
|
|
* @param string input string |
253
|
|
|
* @param integer offset |
254
|
|
|
* @param integer length limit |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
View Code Duplication |
public static function substr($str, $offset, $length = null) |
|
|
|
|
258
|
|
|
{ |
259
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
260
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
261
|
|
|
|
262
|
|
|
// Function has been called |
263
|
|
|
self::$called[__FUNCTION__] = true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return _substr($str, $offset, $length); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Replaces text within a portion of a UTF-8 string. |
271
|
|
|
* @see http://php.net/substr_replace |
272
|
|
|
* |
273
|
|
|
* @author Harry Fuecks <[email protected]> |
274
|
|
|
* |
275
|
|
|
* @param string input string |
276
|
|
|
* @param string replacement string |
277
|
|
|
* @param integer offset |
278
|
|
|
* @return string |
279
|
|
|
*/ |
280
|
|
View Code Duplication |
public static function substr_replace($str, $replacement, $offset, $length = null) |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
283
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
284
|
|
|
|
285
|
|
|
// Function has been called |
286
|
|
|
self::$called[__FUNCTION__] = true; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return _substr_replace($str, $replacement, $offset, $length); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Makes a UTF-8 string lowercase. |
294
|
|
|
* @see http://php.net/strtolower |
295
|
|
|
* |
296
|
|
|
* @author Andreas Gohr <[email protected]> |
297
|
|
|
* |
298
|
|
|
* @param string mixed case string |
299
|
|
|
* @return string |
|
|
|
|
300
|
|
|
*/ |
301
|
|
|
public static function strtolower($str) |
302
|
|
|
{ |
303
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
304
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
305
|
|
|
|
306
|
|
|
// Function has been called |
307
|
|
|
self::$called[__FUNCTION__] = true; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
return _strtolower($str); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Makes a UTF-8 string uppercase. |
315
|
|
|
* @see http://php.net/strtoupper |
316
|
|
|
* |
317
|
|
|
* @author Andreas Gohr <[email protected]> |
318
|
|
|
* |
319
|
|
|
* @param string mixed case string |
320
|
|
|
* @param string $str |
321
|
|
|
* @return string |
|
|
|
|
322
|
|
|
*/ |
323
|
|
|
public static function strtoupper($str) |
324
|
|
|
{ |
325
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
326
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
327
|
|
|
|
328
|
|
|
// Function has been called |
329
|
|
|
self::$called[__FUNCTION__] = true; |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
return _strtoupper($str); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
/** |
336
|
|
|
* Makes a UTF-8 string's first character uppercase. |
337
|
|
|
* @see http://php.net/ucfirst |
338
|
|
|
* |
339
|
|
|
* @author Harry Fuecks <[email protected]> |
340
|
|
|
* |
341
|
|
|
* @param string mixed case string |
342
|
|
|
* @return string |
343
|
|
|
*/ |
344
|
|
|
public static function ucfirst($str) |
345
|
|
|
{ |
346
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
347
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
348
|
|
|
|
349
|
|
|
// Function has been called |
350
|
|
|
self::$called[__FUNCTION__] = true; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
return _ucfirst($str); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Makes the first character of every word in a UTF-8 string uppercase. |
358
|
|
|
* @see http://php.net/ucwords |
359
|
|
|
* |
360
|
|
|
* @author Harry Fuecks <[email protected]> |
361
|
|
|
* |
362
|
|
|
* @param string mixed case string |
363
|
|
|
* @return string |
364
|
|
|
*/ |
365
|
|
|
public static function ucwords($str) |
366
|
|
|
{ |
367
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
368
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
369
|
|
|
|
370
|
|
|
// Function has been called |
371
|
|
|
self::$called[__FUNCTION__] = true; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
return _ucwords($str); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
/** |
378
|
|
|
* Case-insensitive UTF-8 string comparison. |
379
|
|
|
* @see http://php.net/strcasecmp |
380
|
|
|
* |
381
|
|
|
* @author Harry Fuecks <[email protected]> |
382
|
|
|
* |
383
|
|
|
* @param string string to compare |
384
|
|
|
* @param string string to compare |
385
|
|
|
* @return integer less than 0 if str1 is less than str2 |
386
|
|
|
* @return integer greater than 0 if str1 is greater than str2 |
387
|
|
|
* @return integer 0 if they are equal |
388
|
|
|
*/ |
389
|
|
|
public static function strcasecmp($str1, $str2) |
390
|
|
|
{ |
391
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
392
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
393
|
|
|
|
394
|
|
|
// Function has been called |
395
|
|
|
self::$called[__FUNCTION__] = true; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return _strcasecmp($str1, $str2); |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
/** |
402
|
|
|
* Returns a string or an array with all occurrences of search in subject (ignoring case). |
403
|
|
|
* replaced with the given replace value. |
404
|
|
|
* @see http://php.net/str_ireplace |
405
|
|
|
* |
406
|
|
|
* @note It's not fast and gets slower if $search and/or $replace are arrays. |
407
|
|
|
* @author Harry Fuecks <[email protected] |
408
|
|
|
* |
409
|
|
|
* @param string|array text to replace |
410
|
|
|
* @param string|array replacement text |
411
|
|
|
* @param string|array subject text |
412
|
|
|
* @param integer number of matched and replaced needles will be returned via this parameter which is passed by reference |
413
|
|
|
* @return string if the input was a string |
414
|
|
|
* @return array if the input was an array |
415
|
|
|
*/ |
416
|
|
View Code Duplication |
public static function str_ireplace($search, $replace, $str, & $count = null) |
|
|
|
|
417
|
|
|
{ |
418
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
419
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
420
|
|
|
|
421
|
|
|
// Function has been called |
422
|
|
|
self::$called[__FUNCTION__] = true; |
423
|
|
|
} |
424
|
|
|
|
425
|
|
|
return _str_ireplace($search, $replace, $str, $count); |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Case-insenstive UTF-8 version of strstr. Returns all of input string |
430
|
|
|
* from the first occurrence of needle to the end. |
431
|
|
|
* @see http://php.net/stristr |
432
|
|
|
* |
433
|
|
|
* @author Harry Fuecks <[email protected]> |
434
|
|
|
* |
435
|
|
|
* @param string input string |
436
|
|
|
* @param string needle |
437
|
|
|
* @return string matched substring if found |
438
|
|
|
* @return boolean FALSE if the substring was not found |
439
|
|
|
*/ |
440
|
|
|
public static function stristr($str, $search) |
441
|
|
|
{ |
442
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
443
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
444
|
|
|
|
445
|
|
|
// Function has been called |
446
|
|
|
self::$called[__FUNCTION__] = true; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
return _stristr($str, $search); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* Finds the length of the initial segment matching mask. |
454
|
|
|
* @see http://php.net/strspn |
455
|
|
|
* |
456
|
|
|
* @author Harry Fuecks <[email protected]> |
457
|
|
|
* |
458
|
|
|
* @param string input string |
459
|
|
|
* @param string mask for search |
460
|
|
|
* @param integer start position of the string to examine |
461
|
|
|
* @param integer length of the string to examine |
462
|
|
|
* @return integer length of the initial segment that contains characters in the mask |
463
|
|
|
*/ |
464
|
|
View Code Duplication |
public static function strspn($str, $mask, $offset = null, $length = null) |
|
|
|
|
465
|
|
|
{ |
466
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
467
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
468
|
|
|
|
469
|
|
|
// Function has been called |
470
|
|
|
self::$called[__FUNCTION__] = true; |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
return _strspn($str, $mask, $offset, $length); |
474
|
|
|
} |
475
|
|
|
|
476
|
|
|
/** |
477
|
|
|
* Finds the length of the initial segment not matching mask. |
478
|
|
|
* @see http://php.net/strcspn |
479
|
|
|
* |
480
|
|
|
* @author Harry Fuecks <[email protected]> |
481
|
|
|
* |
482
|
|
|
* @param string input string |
483
|
|
|
* @param string mask for search |
484
|
|
|
* @param integer start position of the string to examine |
485
|
|
|
* @param integer length of the string to examine |
486
|
|
|
* @return integer length of the initial segment that contains characters not in the mask |
487
|
|
|
*/ |
488
|
|
View Code Duplication |
public static function strcspn($str, $mask, $offset = null, $length = null) |
|
|
|
|
489
|
|
|
{ |
490
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
491
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
492
|
|
|
|
493
|
|
|
// Function has been called |
494
|
|
|
self::$called[__FUNCTION__] = true; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
return _strcspn($str, $mask, $offset, $length); |
498
|
|
|
} |
499
|
|
|
|
500
|
|
|
/** |
501
|
|
|
* Pads a UTF-8 string to a certain length with another string. |
502
|
|
|
* @see http://php.net/str_pad |
503
|
|
|
* |
504
|
|
|
* @author Harry Fuecks <[email protected]> |
505
|
|
|
* |
506
|
|
|
* @param string input string |
507
|
|
|
* @param integer desired string length after padding |
508
|
|
|
* @param string string to use as padding |
509
|
|
|
* @param string padding type: STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH |
510
|
|
|
* @return string |
511
|
|
|
*/ |
512
|
|
View Code Duplication |
public static function str_pad($str, $final_str_length, $pad_str = ' ', $pad_type = STR_PAD_RIGHT) |
|
|
|
|
513
|
|
|
{ |
514
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
515
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
516
|
|
|
|
517
|
|
|
// Function has been called |
518
|
|
|
self::$called[__FUNCTION__] = true; |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
return _str_pad($str, $final_str_length, $pad_str, $pad_type); |
522
|
|
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* Converts a UTF-8 string to an array. |
526
|
|
|
* @see http://php.net/str_split |
527
|
|
|
* |
528
|
|
|
* @author Harry Fuecks <[email protected]> |
529
|
|
|
* |
530
|
|
|
* @param string input string |
531
|
|
|
* @param integer maximum length of each chunk |
532
|
|
|
* @param string $str |
533
|
|
|
* @return array |
534
|
|
|
*/ |
535
|
|
|
public static function str_split($str, $split_length = 1) |
536
|
|
|
{ |
537
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
538
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
539
|
|
|
|
540
|
|
|
// Function has been called |
541
|
|
|
self::$called[__FUNCTION__] = true; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
return _str_split($str, $split_length); |
545
|
|
|
} |
546
|
|
|
|
547
|
|
|
/** |
548
|
|
|
* Reverses a UTF-8 string. |
549
|
|
|
* @see http://php.net/strrev |
550
|
|
|
* |
551
|
|
|
* @author Harry Fuecks <[email protected]> |
552
|
|
|
* |
553
|
|
|
* @param string string to be reversed |
554
|
|
|
* @return string |
555
|
|
|
*/ |
556
|
|
|
public static function strrev($str) |
557
|
|
|
{ |
558
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
559
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
560
|
|
|
|
561
|
|
|
// Function has been called |
562
|
|
|
self::$called[__FUNCTION__] = true; |
563
|
|
|
} |
564
|
|
|
|
565
|
|
|
return _strrev($str); |
566
|
|
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Strips whitespace (or other UTF-8 characters) from the beginning and |
570
|
|
|
* end of a string. |
571
|
|
|
* @see http://php.net/trim |
572
|
|
|
* |
573
|
|
|
* @author Andreas Gohr <[email protected]> |
574
|
|
|
* |
575
|
|
|
* @param string input string |
576
|
|
|
* @param string string of characters to remove |
577
|
|
|
* @return string |
578
|
|
|
*/ |
579
|
|
View Code Duplication |
public static function trim($str, $charlist = null) |
|
|
|
|
580
|
|
|
{ |
581
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
582
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
583
|
|
|
|
584
|
|
|
// Function has been called |
585
|
|
|
self::$called[__FUNCTION__] = true; |
586
|
|
|
} |
587
|
|
|
|
588
|
|
|
return _trim($str, $charlist); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Strips whitespace (or other UTF-8 characters) from the beginning of a string. |
593
|
|
|
* @see http://php.net/ltrim |
594
|
|
|
* |
595
|
|
|
* @author Andreas Gohr <[email protected]> |
596
|
|
|
* |
597
|
|
|
* @param string input string |
598
|
|
|
* @param string string of characters to remove |
599
|
|
|
* @param string $str |
600
|
|
|
* @return string |
601
|
|
|
*/ |
602
|
|
View Code Duplication |
public static function ltrim($str, $charlist = null) |
|
|
|
|
603
|
|
|
{ |
604
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
605
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
606
|
|
|
|
607
|
|
|
// Function has been called |
608
|
|
|
self::$called[__FUNCTION__] = true; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
return _ltrim($str, $charlist); |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Strips whitespace (or other UTF-8 characters) from the end of a string. |
616
|
|
|
* @see http://php.net/rtrim |
617
|
|
|
* |
618
|
|
|
* @author Andreas Gohr <[email protected]> |
619
|
|
|
* |
620
|
|
|
* @param string input string |
621
|
|
|
* @param string string of characters to remove |
622
|
|
|
* @return string |
623
|
|
|
*/ |
624
|
|
View Code Duplication |
public static function rtrim($str, $charlist = null) |
|
|
|
|
625
|
|
|
{ |
626
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
627
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
628
|
|
|
|
629
|
|
|
// Function has been called |
630
|
|
|
self::$called[__FUNCTION__] = true; |
631
|
|
|
} |
632
|
|
|
|
633
|
|
|
return _rtrim($str, $charlist); |
634
|
|
|
} |
635
|
|
|
|
636
|
|
|
/** |
637
|
|
|
* Returns the unicode ordinal for a character. |
638
|
|
|
* @see http://php.net/ord |
639
|
|
|
* |
640
|
|
|
* @author Harry Fuecks <[email protected]> |
641
|
|
|
* |
642
|
|
|
* @param string UTF-8 encoded character |
643
|
|
|
* @return integer |
|
|
|
|
644
|
|
|
*/ |
645
|
|
|
public static function ord($chr) |
646
|
|
|
{ |
647
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
648
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
649
|
|
|
|
650
|
|
|
// Function has been called |
651
|
|
|
self::$called[__FUNCTION__] = true; |
652
|
|
|
} |
653
|
|
|
|
654
|
|
|
return _ord($chr); |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
/** |
658
|
|
|
* Takes an UTF-8 string and returns an array of ints representing the Unicode characters. |
659
|
|
|
* Astral planes are supported i.e. the ints in the output can be > 0xFFFF. |
660
|
|
|
* Occurrances of the BOM are ignored. Surrogates are not allowed. |
661
|
|
|
* |
662
|
|
|
* The Original Code is Mozilla Communicator client code. |
663
|
|
|
* The Initial Developer of the Original Code is Netscape Communications Corporation. |
664
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer. |
665
|
|
|
* Ported to PHP by Henri Sivonen <[email protected]>, see http://hsivonen.iki.fi/php-utf8/. |
666
|
|
|
* Slight modifications to fit with phputf8 library by Harry Fuecks <[email protected]>. |
667
|
|
|
* |
668
|
|
|
* @param string UTF-8 encoded string |
669
|
|
|
* @return array unicode code points |
670
|
|
|
* @return boolean FALSE if the string is invalid |
|
|
|
|
671
|
|
|
*/ |
672
|
|
|
public static function to_unicode($str) |
673
|
|
|
{ |
674
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
675
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
676
|
|
|
|
677
|
|
|
// Function has been called |
678
|
|
|
self::$called[__FUNCTION__] = true; |
679
|
|
|
} |
680
|
|
|
|
681
|
|
|
return _to_unicode($str); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* Takes an array of ints representing the Unicode characters and returns a UTF-8 string. |
686
|
|
|
* Astral planes are supported i.e. the ints in the input can be > 0xFFFF. |
687
|
|
|
* Occurrances of the BOM are ignored. Surrogates are not allowed. |
688
|
|
|
* |
689
|
|
|
* The Original Code is Mozilla Communicator client code. |
690
|
|
|
* The Initial Developer of the Original Code is Netscape Communications Corporation. |
691
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998 the Initial Developer. |
692
|
|
|
* Ported to PHP by Henri Sivonen <[email protected]>, see http://hsivonen.iki.fi/php-utf8/. |
693
|
|
|
* Slight modifications to fit with phputf8 library by Harry Fuecks <[email protected]>. |
694
|
|
|
* |
695
|
|
|
* @param array unicode code points representing a string |
696
|
|
|
* @return string utf8 string of characters |
697
|
|
|
* @return boolean FALSE if a code point cannot be found |
|
|
|
|
698
|
|
|
*/ |
699
|
|
|
public static function from_unicode($arr) |
700
|
|
|
{ |
701
|
|
|
if (! isset(self::$called[__FUNCTION__])) { |
702
|
|
|
require SYSPATH.'core/utf8/'.__FUNCTION__.EXT; |
703
|
|
|
|
704
|
|
|
// Function has been called |
705
|
|
|
self::$called[__FUNCTION__] = true; |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
return _from_unicode($arr); |
709
|
|
|
} |
710
|
|
|
} // End utf8 |
711
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.