1
|
|
|
<?php |
2
|
|
|
namespace morphos; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Multibyte string helper |
6
|
|
|
*/ |
7
|
|
|
class S { |
8
|
|
|
/** |
9
|
|
|
* Sets encoding for using in morphos/* functions. |
10
|
|
|
*/ |
11
|
|
|
static public function set_encoding($encoding) { |
12
|
|
|
if (function_exists('mb_internal_encoding')) { |
13
|
|
|
mb_internal_encoding($encoding); |
14
|
|
|
} else if (function_exists('iconv_set_encoding')) { |
15
|
|
|
iconv_set_encoding('internal_encoding', $encoding); |
16
|
|
|
} else { |
17
|
|
|
return false; |
18
|
|
|
} |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Calcules count of characters in string. |
23
|
|
|
*/ |
24
|
|
|
static public function length($string) { |
25
|
|
|
if (function_exists('mb_strlen')) { |
26
|
|
|
return mb_strlen($string); |
27
|
|
|
} else if (function_exists('iconv_strlen')) { |
28
|
|
|
return iconv_strlen($string); |
29
|
|
|
} else { |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Slices string like python. |
36
|
|
|
*/ |
37
|
|
|
static public function slice($string, $start, $end = null) { |
38
|
|
|
if ($end != null) { |
39
|
|
|
$end -= $start; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (function_exists('mb_substr')) { |
43
|
|
|
return $end === null ? mb_substr($string, $start) : mb_substr($string, $start, $end); |
44
|
|
|
} else if (function_exists('iconv_substr')) { |
45
|
|
|
return $end === null ? iconv_substr($string, $start) : iconv_substr($string, $start, $end); |
46
|
|
|
} else { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Lower case. |
53
|
|
|
*/ |
54
|
|
|
static public function lower($string) { |
55
|
|
|
if (function_exists('mb_strtolower')) { |
56
|
|
|
return mb_strtolower($string); |
57
|
|
|
} else { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Upper case. |
64
|
|
|
*/ |
65
|
|
|
static public function upper($string) { |
66
|
|
|
if (function_exists('mb_strtoupper')) { |
67
|
|
|
return mb_strtoupper($string); |
68
|
|
|
} else { |
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Name case. (ex: Thomas, Lewis) |
75
|
|
|
*/ |
76
|
|
|
static public function name($string) { |
77
|
|
|
if (function_exists('mb_strtoupper')) { |
78
|
|
|
return implode('-', array_map(function ($word) { return self::upper(self::slice($word, 0, 1)).self::lower(self::slice($word, 1)); }, explode('-', $string))); |
79
|
|
|
} else { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* multiple substr_count(). |
86
|
|
|
*/ |
87
|
|
|
static public function chars_count($string, array $chars) { |
88
|
|
|
if (function_exists('mb_split')) { |
89
|
|
|
return count(mb_split('('.implode('|', $chars).')', $string)) - 1; |
90
|
|
|
} else { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
static public function last_position_for_one_of_chars($string, array $chars) { |
96
|
|
|
if (function_exists('mb_strrpos')) { |
97
|
|
|
$last_position = false; |
98
|
|
|
foreach ($chars as $char) { |
99
|
|
|
if (($pos = mb_strrpos($string, $char)) !== false) { |
100
|
|
|
if ($pos > $last_position) |
101
|
|
|
$last_position = $pos; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
if ($last_position !== false) { |
105
|
|
|
return mb_substr($string, $last_position); |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} else { |
109
|
|
|
return false; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
static public function indexOf($string, $substring, $caseSensetive = false, $startOffset = 0) { |
114
|
|
|
if (function_exists('mb_stripos')) { |
115
|
|
|
return $caseSensetive ? mb_strpos($string, $substring, $startOffset) : mb_stripos($string, $substring, $startOffset); |
116
|
|
|
} else { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|