19
19
use function str_ends_with ;
20
20
use function str_starts_with ;
21
21
use function stripos ;
22
+ use function strlen ;
22
23
use function strpos ;
23
24
use function strrpos ;
24
25
use function strtolower ;
@@ -276,7 +277,64 @@ public static function strrpos(string $str, string $find, int $offset = 0, strin
276
277
}
277
278
278
279
/**
279
- * Assert is start withs one
280
+ * @param string $str
281
+ * @param string $needle
282
+ *
283
+ * @return bool
284
+ */
285
+ public static function startWith (string $ str , string $ needle ): bool
286
+ {
287
+ return self ::hasPrefix ($ str , $ needle );
288
+ }
289
+
290
+ /**
291
+ * @param string $str
292
+ * @param string $needle
293
+ *
294
+ * @return bool
295
+ */
296
+ public static function hasPrefix (string $ str , string $ needle ): bool
297
+ {
298
+ return str_starts_with ($ str , $ needle );
299
+ }
300
+
301
+ /**
302
+ * @param string $str
303
+ * @param string $needle
304
+ *
305
+ * @return bool
306
+ */
307
+ public static function isStartWithIC (string $ str , string $ needle ): bool
308
+ {
309
+ return self ::hasPrefixIC ($ str , $ needle );
310
+ }
311
+
312
+ /**
313
+ * @param string $str
314
+ * @param string $needle
315
+ *
316
+ * @return bool
317
+ */
318
+ public static function startWithIC (string $ str , string $ needle ): bool
319
+ {
320
+ return self ::hasPrefixIC ($ str , $ needle );
321
+ }
322
+
323
+ /**
324
+ * ignore case, the passed $str ends with the $needle string
325
+ *
326
+ * @param string $str
327
+ * @param string $needle
328
+ *
329
+ * @return bool
330
+ */
331
+ public static function hasPrefixIC (string $ str , string $ needle ): bool
332
+ {
333
+ return stripos ($ str , $ needle ) === 0 ;
334
+ }
335
+
336
+ /**
337
+ * check $str is start withs one of $needle
280
338
*
281
339
* @param string $str
282
340
* @param string|array $needle
@@ -299,13 +357,13 @@ public static function isStartWiths(string $str, $needle): bool
299
357
300
358
/**
301
359
* @param string $str
302
- * @param string $needle
360
+ * @param string|array $needle
303
361
*
304
362
* @return bool
305
363
*/
306
- public static function hasPrefix (string $ str , string $ needle ): bool
364
+ public static function startWiths (string $ str , $ needle ): bool
307
365
{
308
- return str_starts_with ($ str , $ needle );
366
+ return self :: isStartWiths ($ str , $ needle );
309
367
}
310
368
311
369
/**
@@ -320,14 +378,16 @@ public static function hasPrefixes(string $str, array $needles): bool
320
378
}
321
379
322
380
/**
381
+ * the passed $str ends with the $needle string
382
+ *
323
383
* @param string $str
324
384
* @param string $needle
325
385
*
326
386
* @return bool
327
387
*/
328
- public static function isEndWiths (string $ str , string $ needle ): bool
388
+ public static function hasSuffix (string $ str , string $ needle ): bool
329
389
{
330
- return self :: hasSuffix ($ str , $ needle );
390
+ return str_ends_with ($ str , $ needle );
331
391
}
332
392
333
393
/**
@@ -336,9 +396,68 @@ public static function isEndWiths(string $str, string $needle): bool
336
396
*
337
397
* @return bool
338
398
*/
339
- public static function hasSuffix (string $ str , string $ needle ): bool
399
+ public static function endWithIC (string $ str , string $ needle ): bool
340
400
{
341
- return str_ends_with ($ str , $ needle );
401
+ return self ::hasSuffixIC ($ str , $ needle );
402
+ }
403
+
404
+ /**
405
+ * ignore case, check $str is ends with the $needle string
406
+ *
407
+ * @param string $str
408
+ * @param string $needle
409
+ *
410
+ * @return bool
411
+ */
412
+ public static function hasSuffixIC (string $ str , string $ needle ): bool
413
+ {
414
+ $ pos = stripos ($ str , $ needle );
415
+
416
+ return $ pos !== false && $ pos + strlen ($ needle ) === strlen ($ str );
417
+ }
418
+
419
+ /**
420
+ * @param string $str
421
+ * @param string|array $needle
422
+ *
423
+ * @return bool
424
+ */
425
+ public static function endWiths (string $ str , $ needle ): bool
426
+ {
427
+ return self ::hasSuffixes ($ str , $ needle );
428
+ }
429
+
430
+ /**
431
+ * @param string $str
432
+ * @param string|array $needle
433
+ *
434
+ * @return bool
435
+ */
436
+ public static function isEndWiths (string $ str , $ needle ): bool
437
+ {
438
+ return self ::hasSuffixes ($ str , $ needle );
439
+ }
440
+
441
+ /**
442
+ * Assert is start withs one
443
+ *
444
+ * @param string $str
445
+ * @param string|array $needles
446
+ *
447
+ * @return bool
448
+ */
449
+ public static function hasSuffixes (string $ str , $ needles ): bool
450
+ {
451
+ if (is_array ($ needles )) {
452
+ foreach ($ needles as $ needle ) {
453
+ if (str_ends_with ($ str , $ needle )) {
454
+ return true ;
455
+ }
456
+ }
457
+ return false ;
458
+ }
459
+
460
+ return str_ends_with ($ str , $ needles );
342
461
}
343
462
344
463
/**
0 commit comments