1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\SslCertificate; |
4
|
|
|
|
5
|
|
View Code Duplication |
function starts_with($haystack, $needles): bool |
|
|
|
|
6
|
|
|
{ |
7
|
|
|
foreach ((array) $needles as $needle) { |
8
|
|
|
if ($needle != '' && mb_strpos($haystack, $needle) === 0) { |
9
|
|
|
return true; |
10
|
|
|
} |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
return false; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Determine if a given string ends with a given substring. |
18
|
|
|
* |
19
|
|
|
* @param string $haystack |
20
|
|
|
* @param string|array $needles |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
function ends_with(string $haystack, $needles): bool |
25
|
|
|
{ |
26
|
|
|
foreach ((array) $needles as $needle) { |
27
|
|
|
if ((string) $needle === substr($haystack, -length($needle))) { |
28
|
|
|
return true; |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns the portion of string specified by the start and length parameters. |
37
|
|
|
* |
38
|
|
|
* @param string $string |
39
|
|
|
* @param int $start |
40
|
|
|
* @param int|null $length |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
function substr(string $string, int $start, int $length = null): string |
45
|
|
|
{ |
46
|
|
|
return mb_substr($string, $start, $length, 'UTF-8'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the length of the given string. |
51
|
|
|
* |
52
|
|
|
* @param string $value |
53
|
|
|
* |
54
|
|
|
* @return int |
55
|
|
|
*/ |
56
|
|
|
function length(string $value): int |
57
|
|
|
{ |
58
|
|
|
return mb_strlen($value); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Determine if a given string contains a given substring. |
63
|
|
|
* |
64
|
|
|
* @param string $haystack |
65
|
|
|
* @param string|array $needles |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
function str_contains(string $haystack, $needles): bool |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
foreach ((array) $needles as $needle) { |
72
|
|
|
if ($needle != '' && mb_strpos($haystack, $needle) !== false) { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.