1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\Utils; |
3
|
|
|
|
4
|
|
|
final class Strings |
5
|
|
|
{ |
6
|
|
|
public static function contains($haystack, $needle, $ignoreCase = true) |
7
|
|
|
{ |
8
|
|
|
if (false !== $ignoreCase) { |
9
|
|
|
$function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos'; |
10
|
|
|
} else { |
11
|
|
|
$function = function_exists('mb_strpos') ? 'mb_strpos' : 'strpos'; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
return false !== $function($haystack, $needle); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
public static function endsWith($haystack, $needle) |
18
|
|
|
{ |
19
|
|
|
$functionSubstr = function_exists('mb_substr') ? 'mb_substr' : 'substr'; |
20
|
|
|
$functionStrlen = function_exists('mb_strlen') ? 'mb_strlen' : 'strlen'; |
21
|
|
|
$check = $functionSubstr($haystack, $functionStrlen($haystack) - ($functionStrlen($needle))); |
22
|
|
|
return $check == $needle; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function getSlug($string) |
26
|
|
|
{ |
27
|
|
|
$transliterator = \Transliterator::createFromRules( |
28
|
|
|
':: Any-Latin;' |
29
|
|
|
. ':: NFD;' |
30
|
|
|
. ':: [:Nonspacing Mark:] Remove;' |
31
|
|
|
. ':: NFC;' |
32
|
|
|
. ':: [:Punctuation:] Remove;' |
33
|
|
|
. ':: Lower();' |
34
|
|
|
. '[:Separator:] > \'-\'' |
35
|
|
|
); |
36
|
|
|
if (!($transliterator instanceof \Transliterator)) { |
|
|
|
|
37
|
|
|
throw new \WebServCo\Framework\Exceptions\ApplicationException('Transliterator error.'); |
38
|
|
|
} |
39
|
|
|
return $transliterator->transliterate($string); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function startsWith($haystack, $needle, $ignoreCase = true) |
43
|
|
|
{ |
44
|
|
|
if (false !== $ignoreCase) { |
45
|
|
|
$function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos'; |
46
|
|
|
} else { |
47
|
|
|
$function = function_exists('mb_strpos') ? 'mb_strpos' : 'strpos'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return 0 === $function($haystack, $needle); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function stripNonDigits($haystack) |
54
|
|
|
{ |
55
|
|
|
return preg_replace("/\D+/", '', $haystack); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|