Str.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* LearnDash Strings helper class.
*
* @since 4.6.0
*
* @package LearnDash\Core
*/
/** NOTICE: This code is currently under development and may not be stable.
* Its functionality, behavior, and interfaces may change at any time without notice.
* Please refrain from using it in production or other critical systems.
* By using this code, you assume all risks and liabilities associated with its use.
* Thank you for your understanding and cooperation.
**/
namespace LearnDash\Core\Utilities;
/**
* A helper class to provide string manipulation methods.
*
* @since 4.6.0
*/
class Str {
/**
* Replaces the first occurrence of a given value in the string.
*
* @since 4.6.0
*
* @param string $search The string to search for and replace.
* @param string $replace The replacement string.
* @param string $subject The string to do the search and replace from.
*
* @return string The string with the first occurrence of a given value replaced.
*/
public static function replace_first( string $search, string $replace, string $subject ): string {
if ( '' === $search ) {
return $subject;
}
$position = strpos( $subject, $search );
if ( $position !== false ) {
return substr_replace( $subject, $replace, $position, strlen( $search ) );
}
return $subject;
}
/**
* Replaces the last occurrence of a given value in the string.
*
* @since 4.6.0
*
* @param string $search The string to search for and replace.
* @param string $replace The replacement string.
* @param string $subject The string to do the search and replace from.
*
* @return string The string with the last occurrence of a given value replaced.
*/
public static function replace_last( string $search, string $replace, string $subject ): string {
if ( '' === $search ) {
return $subject;
}
$position = strrpos( $subject, $search );
if ( $position !== false ) {
return substr_replace( $subject, $replace, $position, strlen( $search ) );
}
return $subject;
}
/**
* Determines if a given string starts with a given substring.
* Supports multiple needles.
*
* @since 4.6.0
*
* @param string $haystack Haystack.
* @param string|string[] $needles Needle can be a string or an array of strings.
*
* @return bool
*/
public static function starts_with( string $haystack, $needles ): bool {
if ( ! is_array( $needles ) ) {
$needles = array( $needles );
}
foreach ( $needles as $needle ) {
if ( $needle !== '' && 0 === mb_strpos( $haystack, $needle ) ) {
return true;
}
}
return false;
}
/**
* Determines if a given string contains a given substring.
* Supports multiple needles.
*
* @since 4.6.0
*
* @param string $haystack The string to search in.
* @param string|string[] $needles The string to search for.
* @param bool $ignore_case Whether to ignore case. Default false.
*
* @return bool
*/
public static function contains( string $haystack, $needles, bool $ignore_case = false ): bool {
if ( $ignore_case ) {
$haystack = mb_strtolower( $haystack );
}
if ( ! is_array( $needles ) ) {
$needles = (array) $needles;
}
foreach ( $needles as $needle ) {
if ( $ignore_case ) {
$needle = mb_strtolower( $needle );
}
if ( $needle !== '' && false !== mb_strpos( $haystack, $needle ) ) {
return true;
}
}
return false;
}
/**
* Determine if a given string contains all needles.
*
* @since 4.6.0
*
* @param string $haystack The string to search in.
* @param string[] $needles The string to search for.
* @param bool $ignore_case Whether to ignore case. Default false.
*
* @return bool
*/
public static function contains_all( string $haystack, array $needles, bool $ignore_case = false ): bool {
foreach ( $needles as $needle ) {
if ( ! static::contains( $haystack, $needle, $ignore_case ) ) {
return false;
}
}
return true;
}
/**
* Masks a portion of a string with a repeated character.
*
* @since 4.6.0
*
* @param string $string The input string.
* @param string $character The character to use for masking.
* @param int $index The index of the first character to mask.
* @param int|null $length The number of characters to mask. If null, masks until the end of the string.
* @param string $encoding The encoding of the string. Default UTF-8.
*
* @return string
*/
public static function mask( $string, $character, $index, $length = null, $encoding = 'UTF-8' ) {
if ( $character === '' ) {
return $string;
}
$segment = mb_substr( $string, $index, $length, $encoding );
if ( $segment === '' ) {
return $string;
}
$strlen = mb_strlen( $string, $encoding );
$start_index = $index;
if ( $index < 0 ) {
$start_index = $index < -$strlen ? 0 : $strlen + $index;
}
$start = mb_substr( $string, 0, $start_index, $encoding );
$segment_length = mb_strlen( $segment, $encoding );
$end = mb_substr( $string, $start_index + $segment_length );
return $start . str_repeat( mb_substr( $character, 0, 1, $encoding ), intval( $segment_length ) ) . $end;
}
}