1
|
|
|
<?php |
2
|
|
|
namespace WebServCo\Framework\Utils; |
3
|
|
|
|
4
|
|
|
final class Strings |
5
|
|
|
{ |
6
|
|
|
public static function contains(string $haystack, string $needle, bool $ignoreCase = true) : bool |
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(string $haystack, string $needle) : bool |
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
|
|
|
/** |
26
|
|
|
* @param mixed $context |
27
|
|
|
* @return string |
28
|
|
|
*/ |
29
|
|
|
public static function getContextAsString($context) : string |
30
|
|
|
{ |
31
|
|
|
ob_start(); |
32
|
|
|
var_dump($context); |
|
|
|
|
33
|
|
|
return ob_get_clean(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public static function getSlug(string $string) : string |
37
|
|
|
{ |
38
|
|
|
$transliterator = \Transliterator::createFromRules( |
39
|
|
|
':: Any-Latin;' |
40
|
|
|
. ':: NFD;' |
41
|
|
|
. ':: [:Nonspacing Mark:] Remove;' |
42
|
|
|
. ':: NFC;' |
43
|
|
|
. ':: [:Punctuation:] Remove;' |
44
|
|
|
. ':: Lower();' |
45
|
|
|
. '[:Separator:] > \'-\'' |
46
|
|
|
); |
47
|
|
|
if (!($transliterator instanceof \Transliterator)) { |
|
|
|
|
48
|
|
|
throw new \WebServCo\Framework\Exceptions\ApplicationException('Transliterator error.'); |
49
|
|
|
} |
50
|
|
|
return $transliterator->transliterate($string); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function linkify($string) |
54
|
|
|
{ |
55
|
|
|
return preg_replace( |
56
|
|
|
"~[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]~", |
57
|
|
|
"<a href=\"\\0\">\\0</a>", |
58
|
|
|
$string |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public static function startsWith(string $haystack, string $needle, bool $ignoreCase = true) : bool |
63
|
|
|
{ |
64
|
|
|
if (false !== $ignoreCase) { |
65
|
|
|
$function = function_exists('mb_stripos') ? 'mb_stripos' : 'stripos'; |
66
|
|
|
} else { |
67
|
|
|
$function = function_exists('mb_strpos') ? 'mb_strpos' : 'strpos'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return 0 === $function($haystack, $needle); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public static function stripNonDigits(string $haystack) : string |
74
|
|
|
{ |
75
|
|
|
return preg_replace("/\D+/", '', $haystack); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|