@@ -11,21 +11,23 @@ class Str extends BaseStr
11
11
/**
12
12
* @param $string
13
13
* @param $search
14
+ * @param bool $caseSensitive
14
15
* @return array
15
16
*/
16
- public static function positions ($ string , $ search )
17
+ public static function positions ($ string , $ search, $ caseSensitive = false )
17
18
{
18
- return static ::_position ($ string , $ search );
19
- }
19
+ $ lastPos = 0 ;
20
+ $ positions = [];
21
+ $ length = strlen ($ search );
22
+ $ number = 1 ;
23
+ $ method = $ caseSensitive ? 'stripos ' : 'strpos ' ;
20
24
21
- /**
22
- * @param $string
23
- * @param $search
24
- * @return array
25
- */
26
- public static function ipositions ($ string , $ search )
27
- {
28
- 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 ;
29
31
}
30
32
31
33
/**
@@ -41,10 +43,10 @@ public static function after($subject, $search, $occurrence = 1, $caseSensitive
41
43
return $ subject ;
42
44
}
43
45
44
- $ positions = $ caseSensitive ? static ::positions ($ subject , $ search) : static :: ipositions ( $ subject , $ search );
46
+ $ positions = static ::positions ($ subject , $ search , $ caseSensitive );
45
47
46
48
if ($ occurrence == self ::LAST ) {
47
- $ occurrence = getLastKey ($ positions );
49
+ $ occurrence = Arr:: lastKey ($ positions );
48
50
}
49
51
50
52
if (empty ($ positions [$ occurrence ])) {
@@ -67,10 +69,10 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
67
69
return $ subject ;
68
70
}
69
71
70
- $ positions = $ caseSensitive ? static ::positions ($ subject , $ search) : static :: ipositions ( $ subject , $ search );
72
+ $ positions = static ::positions ($ subject , $ search , $ caseSensitive );
71
73
72
74
if ($ occurrence == self ::LAST ) {
73
- $ occurrence = getLastKey ($ positions );
75
+ $ occurrence = Arr:: lastKey ($ positions );
74
76
}
75
77
76
78
if (empty ($ positions [$ occurrence ])) {
@@ -88,7 +90,7 @@ public static function before($subject, $search, $occurrence = self::LAST, $case
88
90
* @param string $occurenceEnd
89
91
* @return bool|string
90
92
*/
91
- 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 )
92
94
{
93
95
if ($ searchStart == '' || $ searchEnd == '' ) {
94
96
return $ subject ;
@@ -111,24 +113,16 @@ public static function between($subject, $searchStart, $searchEnd, $occurentceSt
111
113
* @param string $left
112
114
* @param string $right
113
115
* @param int $occurence
116
+ * @param bool $caseSensitive
114
117
* @return mixed
115
118
*/
116
- public static function wrap ($ string , $ search , $ left = '' , $ right = '' , $ occurence = 1 )
119
+ public static function wrap ($ string , $ search , $ left = '' , $ right = '' , $ occurence = 1 , $ caseSensitive = false )
117
120
{
118
- return str_replace ($ search , $ left . $ search . $ right , $ string );
119
- }
121
+ if (!$ caseSensitive ) {
122
+ return str_replace ($ search , $ left . $ search . $ right , $ string );
123
+ }
120
124
121
- /**
122
- * @param $string
123
- * @param $search
124
- * @param string $left
125
- * @param string $right
126
- * @param int $occurence
127
- * @return mixed
128
- */
129
- public static function iwrap ($ string , $ search , $ left = '' , $ right = '' , $ occurence = 1 )
130
- {
131
- $ positions = self ::ipositions ($ string , $ search );
125
+ $ positions = self ::positions ($ string , $ search , $ caseSensitive );
132
126
133
127
if (empty ($ positions )) {
134
128
return $ string ;
@@ -147,24 +141,127 @@ public static function iwrap($string, $search, $left = '', $right = '', $occuren
147
141
}
148
142
149
143
/**
150
- * @param $string
151
- * @param $search
152
- * @param bool $caseSensitive
153
- * @return array
144
+ * @param $text
145
+ * @param bool $double
146
+ * @param null $charset
147
+ * @return array|string
154
148
*/
155
- protected static function _position ( $ string , $ search , $ caseSensitive = false )
149
+ public static function h ( $ text , $ double = true , $ charset = null )
156
150
{
157
- $ lastPos = 0 ;
158
- $ positions = [];
159
- $ length = strlen ($ search );
160
- $ number = 1 ;
161
- $ 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
+ }
162
168
163
- while (($ lastPos = $ method ($ string , $ search , $ lastPos ))!== false ) {
164
- $ positions [$ number ++] = $ lastPos ;
165
- $ 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
+ }
166
176
}
167
177
168
- 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> ' ;
169
266
}
170
267
}
0 commit comments