1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sfneal\Helpers\Strings; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
class StringHelpers |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string |
11
|
|
|
*/ |
12
|
|
|
private $string; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* StringHelpers constructor. |
16
|
|
|
* |
17
|
|
|
* @param string $string |
18
|
|
|
*/ |
19
|
|
|
public function __construct(string $string) |
20
|
|
|
{ |
21
|
|
|
$this->string = $string; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sanitize a file name to remove illegal chars. |
26
|
|
|
* |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public function sanitizeFileName(): string |
30
|
|
|
{ |
31
|
|
|
return preg_replace('/[^A-Za-z0-9_\-]/', '_', $this->string); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Truncate a string to be no longer than $chars. |
36
|
|
|
* |
37
|
|
|
* @param int $chars |
38
|
|
|
* @param string $suffix |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function truncate(int $chars = 100, string $suffix = '...'): string |
42
|
|
|
{ |
43
|
|
|
return (strlen($this->string) <= $chars) ? $this->string : substr($this->string, 0, $chars) . $suffix; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Covert a camel cased string into a $char separated string. |
48
|
|
|
* |
49
|
|
|
* @param string $char |
50
|
|
|
* @return string |
51
|
|
|
*/ |
52
|
|
|
public function camelCaseConvert(string $char = '-'): string |
53
|
|
|
{ |
54
|
|
|
return strtolower(preg_replace('/(?<!^)[A-Z]/', $char.'$0', $this->string)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Remove illegal characters from a string to create an ID. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function id(): string |
63
|
|
|
{ |
64
|
|
|
return strtolower( |
65
|
|
|
str_replace(' ', '-', str_replace('&', '-', $this->string)) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Remove ' ', '-', '&' characters. |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function strip() |
75
|
|
|
{ |
76
|
|
|
return strtolower( |
77
|
|
|
str_replace( |
78
|
|
|
',', '', str_replace( |
79
|
|
|
' ', '-', str_replace( |
80
|
|
|
'&', |
81
|
|
|
'', |
82
|
|
|
$this->string |
83
|
|
|
) |
84
|
|
|
) |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Check if a needle string is in a haystack string. |
91
|
|
|
* |
92
|
|
|
* @param string $needle |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function inString(string $needle): bool |
96
|
|
|
{ |
97
|
|
|
return strpos($this->string, $needle) !== false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Determine if the $string is a plain text list of values. |
102
|
|
|
* |
103
|
|
|
* - if $string is a list, an array of values is returned |
104
|
|
|
* - if $sting is a string, false is returned |
105
|
|
|
* |
106
|
|
|
* @param array|string[] $separators |
107
|
|
|
* @return bool|false|string[] |
108
|
|
|
*/ |
109
|
|
|
public function isListString(array $separators = [', ', ' ']) |
110
|
|
|
{ |
111
|
|
|
foreach ($separators as $needle) { |
112
|
|
|
// If the $needle is found in the $string |
113
|
|
|
// we know the string is a plain text list |
114
|
|
|
// so an array of values is returned |
115
|
|
|
if ($this->inString($needle)) { |
116
|
|
|
return explode($needle, $this->string); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// If none of the separators are found |
121
|
|
|
// we know the string is not a list |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Explode a string using an array of delimiters instead of a single string. |
127
|
|
|
* |
128
|
|
|
* @param array|string[] $delimiters |
129
|
|
|
* @param string $replacer |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function explodeMany(array $delimiters = [',', '&', '/', '-'], string $replacer = '***'): array |
133
|
|
|
{ |
134
|
|
|
// Replace all of $delimiters chars with the |
135
|
|
|
// $replacer so that a single delimiter can |
136
|
|
|
// be used to explode the string |
137
|
|
|
$cleaned = str_replace($delimiters, $replacer, $this->string); |
138
|
|
|
|
139
|
|
|
// Use the replacer as the $delimiter |
140
|
|
|
// since it has replaced each of the |
141
|
|
|
// passed $delimiters |
142
|
|
|
$split = explode($replacer, $cleaned); |
143
|
|
|
|
144
|
|
|
// Utilize collection's if laravel/framework is installed |
145
|
|
|
if (function_exists('collect')) { |
146
|
|
|
// Collect the array of strings |
147
|
|
|
return collect($split) |
148
|
|
|
|
149
|
|
|
// Remove empty strings from the collection |
150
|
|
|
->filter(function (string $item) { |
151
|
|
|
return strlen($item) > 0; |
152
|
|
|
}) |
153
|
|
|
|
154
|
|
|
// Trim the remaining strings in the collection |
155
|
|
|
->map(function (string $item) { |
156
|
|
|
return trim($item); |
157
|
|
|
}) |
158
|
|
|
|
159
|
|
|
// Encode as array |
160
|
|
|
->toArray(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Use built in array functions |
164
|
|
|
else { |
165
|
|
|
// Remove empty strings from the collection |
166
|
|
|
$filtered = array_filter($split, function (string $item) { |
167
|
|
|
return strlen($item) > 0; |
168
|
|
|
}); |
169
|
|
|
|
170
|
|
|
// Trim the remaining strings in the collection |
171
|
|
|
return array_map(function (string $item) { |
172
|
|
|
return trim($item); |
173
|
|
|
}, $filtered); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Add whitespace padding to a string. |
179
|
|
|
* |
180
|
|
|
* @param int $amount |
181
|
|
|
* @param string $pad |
182
|
|
|
* @param bool $front add whitespace to 'front' of the string |
183
|
|
|
* @param bool $back add whitespace to the 'back' of the string |
184
|
|
|
* @return string |
185
|
|
|
*/ |
186
|
|
|
public function whitespacePad(int $amount = 1, |
187
|
|
|
string $pad = ' ', |
188
|
|
|
bool $front = true, |
189
|
|
|
bool $back = true) |
190
|
|
|
{ |
191
|
|
|
// Multiple $pad chars by $amount |
192
|
|
|
$whitespace = str_repeat($pad, $amount); |
193
|
|
|
|
194
|
|
|
// Return string with padding |
195
|
|
|
return ($front ? $whitespace : '') . $this->string . ($back ? $whitespace : ''); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Add whitespace padding to only the 'back' of the string. |
200
|
|
|
* |
201
|
|
|
* @param int $amount |
202
|
|
|
* @param string $pad |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function whitespacePadBack(int $amount = 1, string $pad = ' ') |
206
|
|
|
{ |
207
|
|
|
return $this->whitespacePad($amount, $pad, false, true); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Add whitespace padding to only the 'back' of the string. |
212
|
|
|
* |
213
|
|
|
* @param int $amount |
214
|
|
|
* @param string $pad |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
function whitespacePadFront(int $amount = 1, string $pad = ' ') |
|
|
|
|
218
|
|
|
{ |
219
|
|
|
return $this->whitespacePad($amount, $pad, true, false); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.