3
3
namespace LaraSupport ;
4
4
5
5
use Illuminate \Support \Str as BaseStr ;
6
- use function PhpUtil \get_last_key ;
7
6
8
7
class Str extends BaseStr
9
8
{
@@ -12,21 +11,23 @@ class Str extends BaseStr
12
11
/**
13
12
* @param $string
14
13
* @param $search
14
+ * @param bool $caseSensitive
15
15
* @return array
16
16
*/
17
- public static function positions ($ string , $ search )
17
+ public static function positions ($ string , $ search, $ caseSensitive = false )
18
18
{
19
- return static ::_position ($ string , $ search );
20
- }
19
+ $ lastPos = 0 ;
20
+ $ positions = [];
21
+ $ length = strlen ($ search );
22
+ $ number = 1 ;
23
+ $ method = $ caseSensitive ? 'stripos ' : 'strpos ' ;
21
24
22
- /**
23
- * @param $string
24
- * @param $search
25
- * @return array
26
- */
27
- public static function ipositions ($ string , $ search )
28
- {
29
- return static ::_position ($ string , $ search , true );
25
+ while (($ lastPos = $ method ($ string , $ search , $ lastPos ))!== false ) {
26
+ $ positions [$ number ++] = $ lastPos ;
27
+ $ lastPos = $ lastPos + $ length ;
28
+ }
29
+
30
+ return $ positions ;
30
31
}
31
32
32
33
/**
@@ -42,10 +43,10 @@ public static function after($subject, $search, $occurrence = 1, $caseSensitive
42
43
return $ subject ;
43
44
}
44
45
45
- $ positions = $ caseSensitive ? static ::positions ($ subject , $ search) : static :: ipositions ( $ subject , $ search );
46
+ $ positions = static ::positions ($ subject , $ search , $ caseSensitive );
46
47
47
48
if ($ occurrence == self ::LAST ) {
48
- $ occurrence = get_last_key ($ positions );
49
+ $ occurrence = Arr:: lastKey ($ positions );
49
50
}
50
51
51
52
if (empty ($ positions [$ occurrence ])) {
@@ -68,10 +69,10 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
68
69
return $ subject ;
69
70
}
70
71
71
- $ positions = $ caseSensitive ? static ::positions ($ subject , $ search) : static :: ipositions ( $ subject , $ search );
72
+ $ positions = static ::positions ($ subject , $ search , $ caseSensitive );
72
73
73
74
if ($ occurrence == self ::LAST ) {
74
- $ occurrence = get_last_key ($ positions );
75
+ $ occurrence = Arr:: lastKey ($ positions );
75
76
}
76
77
77
78
if (empty ($ positions [$ occurrence ])) {
@@ -89,7 +90,7 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
89
90
* @param string $occurenceEnd
90
91
* @return bool|string
91
92
*/
92
- public static function between ($ subject , $ searchStart , $ searchEnd , $ occurentceStart = 1 , $ occurenceEnd = self ::LAST )
93
+ public static function between ($ subject , $ searchStart , $ searchEnd , $ occurentceStart = 1 , $ occurenceEnd = self ::LAST , $ caseSensitive = false )
93
94
{
94
95
if ($ searchStart == '' || $ searchEnd == '' ) {
95
96
return $ subject ;
@@ -112,24 +113,16 @@ public static function between($subject, $searchStart, $searchEnd, $occurentceSt
112
113
* @param string $left
113
114
* @param string $right
114
115
* @param int $occurence
116
+ * @param bool $caseSensitive
115
117
* @return mixed
116
118
*/
117
- public static function wrap ($ string , $ search , $ left = '' , $ right = '' , $ occurence = 1 )
119
+ public static function wrap ($ string , $ search , $ left = '' , $ right = '' , $ occurence = 1 , $ caseSensitive = false )
118
120
{
119
- return str_replace ($ search , $ left . $ search . $ right , $ string );
120
- }
121
+ if (!$ caseSensitive ) {
122
+ return str_replace ($ search , $ left . $ search . $ right , $ string );
123
+ }
121
124
122
- /**
123
- * @param $string
124
- * @param $search
125
- * @param string $left
126
- * @param string $right
127
- * @param int $occurence
128
- * @return mixed
129
- */
130
- public static function iwrap ($ string , $ search , $ left = '' , $ right = '' , $ occurence = 1 )
131
- {
132
- $ positions = self ::ipositions ($ string , $ search );
125
+ $ positions = self ::positions ($ string , $ search , $ caseSensitive );
133
126
134
127
if (empty ($ positions )) {
135
128
return $ string ;
@@ -148,24 +141,127 @@ public static function iwrap($string, $search, $left = '', $right = '', $occuren
148
141
}
149
142
150
143
/**
151
- * @param $string
152
- * @param $search
153
- * @param bool $caseSensitive
154
- * @return array
144
+ * @param $text
145
+ * @param bool $double
146
+ * @param null $charset
147
+ * @return array|string
155
148
*/
156
- protected static function _position ( $ string , $ search , $ caseSensitive = false )
149
+ public static function h ( $ text , $ double = true , $ charset = null )
157
150
{
158
- $ lastPos = 0 ;
159
- $ positions = [];
160
- $ length = strlen ($ search );
161
- $ number = 1 ;
162
- $ method = $ caseSensitive ? 'stripos ' : 'strpos ' ;
151
+ if (is_string ($ text )) {
152
+ //optimize for strings
153
+ } elseif (is_array ($ text )) {
154
+ $ texts = [];
155
+ foreach ($ text as $ k => $ t ) {
156
+ $ texts [$ k ] = h ($ t , $ double , $ charset );
157
+ }
158
+ return $ texts ;
159
+ } elseif (is_object ($ text )) {
160
+ if (method_exists ($ text , '__toString ' )) {
161
+ $ text = (string )$ text ;
162
+ } else {
163
+ $ text = '(object) ' . get_class ($ text );
164
+ }
165
+ } elseif (is_bool ($ text )) {
166
+ return $ text ;
167
+ }
163
168
164
- while (($ lastPos = $ method ($ string , $ search , $ lastPos ))!== false ) {
165
- $ positions [$ number ++] = $ lastPos ;
166
- $ lastPos = $ lastPos + $ length ;
169
+ static $ defaultCharset = false ;
170
+
171
+ if ($ defaultCharset === false ) {
172
+ $ defaultCharset = mb_internal_encoding ();
173
+ if ($ defaultCharset === null ) {
174
+ $ defaultCharset = 'UTF-8 ' ;
175
+ }
167
176
}
168
177
169
- return $ positions ;
178
+ if (is_string ($ double )) {
179
+ $ charset = $ double ;
180
+ }
181
+
182
+ return htmlspecialchars ($ text , ENT_QUOTES | ENT_SUBSTITUTE , ($ charset ) ? $ charset : $ defaultCharset , $ double );
183
+ }
184
+
185
+ /**
186
+ * returns given amount of characters counting backwards
187
+ *
188
+ * @param string $str
189
+ * @param int $count
190
+ * @return string
191
+ */
192
+ public static function lastChars ($ str , $ count = 1 )
193
+ {
194
+ return mb_substr ($ str , -$ count , $ count );
195
+ }
196
+
197
+ /**
198
+ * create slug from string
199
+ *
200
+ * @param string $str
201
+ * @param string $symbol
202
+ * @return string - e.g. in word1-word2-word3 format
203
+ */
204
+ public static function createSlug ($ str = "" , $ symbol = "- " )
205
+ {
206
+ // if not english
207
+ $ regex = '/^[ -~]+$/ ' ;
208
+ if (!preg_match ($ regex , $ str )) {
209
+ $ str = transliterator_transliterate ('Any-Latin;Latin-ASCII; ' , $ str );
210
+ }
211
+
212
+ $ str = mb_strtolower ($ str );
213
+ $ str = str_replace ("' " , "" , $ str );
214
+ $ str = str_replace ('" ' , "" , $ str );
215
+ $ str = str_replace (". " , $ symbol , $ str );
216
+ $ str = str_replace ("\\" , $ symbol , $ str );
217
+ $ str = str_replace ("/ " , $ symbol , $ str );
218
+ $ str = preg_replace ("/[~\:;\,\?\s\(\)\' \"\[\]\{\}#@&% \$\!\^\+\*=\!\<\>\|?`]/ " , $ symbol , trim ($ str ));
219
+
220
+ // everything but letters and numbers
221
+ $ str = preg_replace ('/(.) \\1{2,}/ ' , '$1 ' , $ str );
222
+
223
+ // letters replace only with 2+ repetition
224
+ $ str = preg_replace ("/[-]{2,}/ " , $ symbol , $ str );
225
+ $ str = rtrim ($ str , $ symbol );
226
+
227
+ return mb_strtolower ($ str );
228
+ }
229
+
230
+ /**
231
+ * @param $val
232
+ * @return string
233
+ */
234
+ public static function _humanize ($ val )
235
+ {
236
+ $ val = str_replace ("_ " , "" , $ val );
237
+ $ matches = preg_split ('/(?=[A-Z])/ ' , $ val );
238
+ return trim (implode (" " , $ matches ));
239
+ }
240
+
241
+ /**
242
+ * returns the short string based on $length if string's length is more than $length
243
+ *
244
+ * @param string $str
245
+ * @param number $length
246
+ * @param bool $raw
247
+ * @return string
248
+ */
249
+ public static function shorten ($ str = '' , $ length = null , $ raw = false )
250
+ {
251
+ if ($ length === null ) {
252
+ $ length = defined ('_PHP_UTIL_SHORTEN_LENGTH ' ) ? _PHP_UTIL_SHORTEN_LENGTH : 50 ;
253
+ }
254
+
255
+ if (mb_strlen ($ str ) > $ length ) {
256
+ $ shortStr = mb_substr ($ str , 0 , $ length ) . "... " ;
257
+
258
+ if ($ raw ) {
259
+ return h ($ shortStr );
260
+ }
261
+ } else {
262
+ return h ($ str );
263
+ }
264
+
265
+ return '<span title=" ' . h (str_ireplace ("/ " , "" , $ str )) . '"> ' . h ($ shortStr ) . '</span> ' ;
170
266
}
171
267
}
0 commit comments