46 public static function isUtf8( $value ) {
47 return mb_check_encoding( (
string)$value,
'UTF-8' );
62 $subject, $nested =
false ) {
66 $encStart = preg_quote( $startDelim,
'!' );
67 $encEnd = preg_quote( $endDelim,
'!' );
68 $encSep = preg_quote( $separator,
'!' );
69 $len = strlen( $subject );
75 "!$encStart|$encEnd|$encSep!S", $subject, $m,
76 PREG_OFFSET_CAPTURE, $inputPos
81 $inputPos = $matchPos + strlen( $match );
82 if ( $match === $separator ) {
85 $subject, $lastPos, $matchPos - $lastPos
89 } elseif ( $match === $startDelim ) {
90 if ( $depth === 0 || $nested ) {
97 $exploded[] = substr( $subject, $lastPos );
100 return new ArrayIterator( $exploded );
121 $segments =
explode( $startDelim, $subject );
122 $output = array_shift( $segments );
123 foreach ( $segments as $s ) {
124 $endDelimPos = strpos( $s, $endDelim );
125 if ( $endDelimPos ===
false ) {
126 $output .= $startDelim . $s;
128 $output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
158 private static function delimiterReplaceCallback( $startDelim, $endDelim, $callback,
159 $subject, $flags =
''
166 $encStart = preg_quote( $startDelim,
'!' );
167 $encEnd = preg_quote( $endDelim,
'!' );
168 $strcmp = strpos( $flags,
'i' ) ===
false ?
'strcmp' :
'strcasecmp';
169 $endLength = strlen( $endDelim );
172 while ( $inputPos < strlen( $subject ) &&
173 preg_match(
"!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
175 $tokenOffset = $m[0][1];
176 if ( $m[1][0] !=
'' ) {
178 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
180 # An end match is present at the same location
182 $tokenLength = $endLength;
184 $tokenType =
'start';
185 $tokenLength = strlen( $m[0][0] );
187 } elseif ( $m[2][0] !=
'' ) {
189 $tokenLength = strlen( $m[0][0] );
191 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
194 if ( $tokenType ==
'start' ) {
195 # Only move the start position if we haven't already found a start
196 # This means that START START END matches outer pair
197 if ( !$foundStart ) {
199 $inputPos = $tokenOffset + $tokenLength;
200 # Write out the non-matching section
201 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
202 $outputPos = $tokenOffset;
203 $contentPos = $inputPos;
206 # Move the input position past the *first character* of START,
207 # to protect against missing END when it overlaps with START
208 $inputPos = $tokenOffset + 1;
210 } elseif ( $tokenType ==
'end' ) {
213 $output .= $callback( [
214 substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
215 substr( $subject, $contentPos, $tokenOffset - $contentPos )
219 # Non-matching end, write it out
220 $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
222 $inputPos = $outputPos = $tokenOffset + $tokenLength;
224 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
227 if ( $outputPos < strlen( $subject ) ) {
228 $output .= substr( $subject, $outputPos );
250 $startDelim, $endDelim, $replace, $subject, $flags =
''
252 return self::delimiterReplaceCallback(
253 $startDelim, $endDelim,
254 static function ( array
$matches ) use ( $replace ) {
270 $placeholder =
"\x00";
273 $text = str_replace( $placeholder,
'', $text );
276 $cleaned = self::delimiterReplaceCallback(
278 static function ( array
$matches ) use ( $search, $placeholder ) {
279 return str_replace( $search, $placeholder,
$matches[0] );
285 $cleaned = str_replace( $search, $replace, $cleaned );
286 $text = str_replace( $placeholder, $search, $cleaned );
301 AtEase::suppressWarnings();
303 $isValid = preg_match( $string,
'' );
304 AtEase::restoreWarnings();
305 return $isValid !==
false;
316 $string = str_replace(
'\\',
'\\\\', $string );
317 return str_replace(
'$',
'\\$', $string );
327 public static function explode( $separator, $subject ) {
328 if ( substr_count( $subject, $separator ) > 1000 ) {
331 return new ArrayIterator(
explode( $separator, $subject ) );
350 public static function unpack(
string $format,
string $data, $length =
false ): array {
351 Assert::parameterType( [
'integer',
'false' ], $length,
'$length' );
352 if ( $length !==
false ) {
353 $realLen = strlen( $data );
354 if ( $realLen < $length ) {
356 .
"string of length $realLen, but needed one "
357 .
"of at least length $length."
362 AtEase::suppressWarnings();
363 $result = unpack( $format, $data );
364 AtEase::restoreWarnings();
366 if ( $result ===
false ) {
static delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags='')
Perform an operation equivalent to preg_replace() with flags.
static delimiterExplode( $startDelim, $endDelim, $separator, $subject, $nested=false)
Explode a string, but ignore any instances of the separator inside the given start and end delimiters...