1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Tin\Util; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* String utility |
9
|
|
|
*/ |
10
|
|
|
class StringUtil |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Remove non alphanumeric chars |
14
|
|
|
* |
15
|
|
|
* @param string $str |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public static function clearString(string $str) |
19
|
|
|
{ |
20
|
|
|
return preg_replace("[^a-zA-Z0-9]", "", $str); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Extract string between two indexes |
25
|
|
|
* |
26
|
|
|
* @param string $str |
27
|
|
|
* @param integer $start |
28
|
|
|
* @param integer $end |
29
|
|
|
* @return string |
30
|
|
|
*/ |
31
|
|
|
public static function substring(string $str, int $start, int $end = null) |
32
|
|
|
{ |
33
|
|
|
if ($end === null) { |
34
|
|
|
return substr($str, $start); |
35
|
|
|
} |
36
|
|
|
return substr($str, $start, $end - $start); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Match length |
41
|
|
|
* |
42
|
|
|
* @param string $str |
43
|
|
|
* @param integer $length |
44
|
|
|
* @return boolean |
45
|
|
|
*/ |
46
|
|
|
public static function isFollowLength(string $str, int $length) |
47
|
|
|
{ |
48
|
|
|
return strlen($str) === $length; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Match a pattern |
53
|
|
|
* |
54
|
|
|
* @param string $str |
55
|
|
|
* @param string $pattern A regex pattern without delimiters |
56
|
|
|
* @return boolean |
57
|
|
|
*/ |
58
|
|
|
public static function isFollowPattern(string $str, string $pattern) |
59
|
|
|
{ |
60
|
|
|
return preg_match("/$pattern/", $str); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get digit at a given position |
65
|
|
|
* |
66
|
|
|
* @param string $str |
67
|
|
|
* @param integer $index |
68
|
|
|
* @return integer |
69
|
|
|
*/ |
70
|
|
|
public static function digitAt(string $str, $index) |
71
|
|
|
{ |
72
|
|
|
return (int) $str[$index]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get numerical value of a letter |
77
|
|
|
* |
78
|
|
|
* eg: A = 10 |
79
|
|
|
* |
80
|
|
|
* @param string $str |
81
|
|
|
* @return int|string |
82
|
|
|
*/ |
83
|
|
|
public static function getNumericValue(string $str) |
84
|
|
|
{ |
85
|
|
|
if (is_numeric($str)) { |
86
|
|
|
return $str; |
87
|
|
|
} |
88
|
|
|
return ord(strtoupper($str)) - 55; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the alphabetical position |
93
|
|
|
* |
94
|
|
|
* eg: A = 1 |
95
|
|
|
* |
96
|
|
|
* @param string $str |
97
|
|
|
* @return integer |
98
|
|
|
*/ |
99
|
|
|
public static function getAlphabeticalPosition(string $str) |
100
|
|
|
{ |
101
|
|
|
$arr = str_split('abcdefghijklmnopqrstuvwxyz'); |
102
|
|
|
$v = array_search(strtolower($str), $arr); |
103
|
|
|
if ($v !== false) { |
104
|
|
|
return $v + 1; |
105
|
|
|
} |
106
|
|
|
return 0; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param string $str |
111
|
|
|
* @return boolean |
112
|
|
|
*/ |
113
|
|
|
public static function isUpper(string $str) |
114
|
|
|
{ |
115
|
|
|
return preg_match('~^\p{Lu}~u', $str) ? true : false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $str |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
public static function isLower(string $str) |
123
|
|
|
{ |
124
|
|
|
return preg_match('~^\p{Ll}~u', $str) ? true : false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param integer $digit |
129
|
|
|
* @param integer $radix |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public static function forDigit(int $digit, int $radix = 10) |
133
|
|
|
{ |
134
|
|
|
if ($radix != 10) { |
135
|
|
|
throw new InvalidArgumentException("Radix should be 10"); |
136
|
|
|
} |
137
|
|
|
return (string) $digit; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $s |
142
|
|
|
* @param string $c |
143
|
|
|
* @param integer $pos |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public static function removesCharacterAtPos(string $s, string $c, int $pos) |
147
|
|
|
{ |
148
|
|
|
$len = strlen($s); |
149
|
|
|
if ($pos > $len - 1) { |
150
|
|
|
return $s; |
151
|
|
|
} |
152
|
|
|
$tmp = $s[$pos]; |
153
|
|
|
if ($tmp == $c) { |
154
|
|
|
$prefix = ""; |
155
|
|
|
if ($pos <= $len) { |
156
|
|
|
$prefix = substr($s, 0, $pos); |
157
|
|
|
} |
158
|
|
|
$suffix = ""; |
159
|
|
|
if ($pos + 1 <= $len - 1) { |
160
|
|
|
$suffix = substr($s, $pos + 1); |
161
|
|
|
} |
162
|
|
|
return $prefix . $suffix; |
163
|
|
|
} |
164
|
|
|
return $s; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $tin |
169
|
|
|
* @param integer $length |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public static function fillWith0UntilLength(string $tin, int $length) |
173
|
|
|
{ |
174
|
|
|
$normalizedTIN = $tin; |
175
|
|
|
while (strlen($normalizedTIN) < $length) { |
176
|
|
|
$normalizedTIN = "0" . $normalizedTIN; |
177
|
|
|
} |
178
|
|
|
return $normalizedTIN; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|