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 getContextAsString($context) |
26
|
|
|
{ |
27
|
|
|
ob_start(); |
28
|
|
|
var_dump($context); |
|
|
|
|
29
|
|
|
return ob_get_clean(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function getSlug($string) |
33
|
|
|
{ |
34
|
|
|
$transliterator = \Transliterator::createFromRules( |
35
|
|
|
':: Any-Latin;' |
36
|
|
|
. ':: NFD;' |
37
|
|
|
. ':: [:Nonspacing Mark:] Remove;' |
38
|
|
|
. ':: NFC;' |
39
|
|
|
. ':: [:Punctuation:] Remove;' |
40
|
|
|
. ':: Lower();' |
41
|
|
|
. '[:Separator:] > \'-\'' |
42
|
|
|
); |
43
|
|
|
if (!($transliterator instanceof \Transliterator)) { |
|
|
|
|
44
|
|
|
throw new \WebServCo\Framework\Exceptions\ApplicationException('Transliterator error.'); |
45
|
|
|
} |
46
|
|
|
return $transliterator->transliterate($string); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public static function startsWith($haystack, $needle, $ignoreCase = true) |
50
|
|
|
{ |
51
|
|
|
if (false !== $ignoreCase) { |
52
|
|
|
$function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos'; |
53
|
|
|
} else { |
54
|
|
|
$function = function_exists('mb_strpos') ? 'mb_strpos' : 'strpos'; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return 0 === $function($haystack, $needle); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public static function stripNonDigits($haystack) |
61
|
|
|
{ |
62
|
|
|
return preg_replace("/\D+/", '', $haystack); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|