44 public static function isUtf8( $value ) {
45 return mb_check_encoding( (
string)$value,
'UTF-8' );
60 $subject, $nested =
false ) {
64 $encStart = preg_quote( $startDelim,
'!' );
65 $encEnd = preg_quote( $endDelim,
'!' );
66 $encSep = preg_quote( $separator,
'!' );
67 $len = strlen( $subject );
73 "!$encStart|$encEnd|$encSep!S", $subject, $m,
74 PREG_OFFSET_CAPTURE, $inputPos
79 $inputPos = $matchPos + strlen( $match );
80 if ( $match === $separator ) {
83 $subject, $lastPos, $matchPos - $lastPos
87 } elseif ( $match === $startDelim ) {
88 if ( $depth === 0 || $nested ) {
95 $exploded[] = substr( $subject, $lastPos );
98 return new ArrayIterator( $exploded );
119 $segments =
explode( $startDelim, $subject );
120 $output = array_shift( $segments );
121 foreach ( $segments as
$s ) {
122 $endDelimPos = strpos(
$s, $endDelim );
123 if ( $endDelimPos ===
false ) {
124 $output .= $startDelim .
$s;
126 $output .= $replace . substr(
$s, $endDelimPos + strlen( $endDelim ) );
157 private static function delimiterReplaceCallback( $startDelim, $endDelim, $callback,
158 $subject, $flags =
''
165 $encStart = preg_quote( $startDelim,
'!' );
166 $encEnd = preg_quote( $endDelim,
'!' );
167 $strcmp = strpos( $flags,
'i' ) ===
false ?
'strcmp' :
'strcasecmp';
168 $endLength = strlen( $endDelim );
171 while ( $inputPos < strlen( $subject ) &&
172 preg_match(
"!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
174 $tokenOffset = $m[0][1];
175 if ( $m[1][0] !=
'' ) {
177 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
179 # An end match is present at the same location
181 $tokenLength = $endLength;
183 $tokenType =
'start';
184 $tokenLength = strlen( $m[0][0] );
186 } elseif ( $m[2][0] !=
'' ) {
188 $tokenLength = strlen( $m[0][0] );
190 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
193 if ( $tokenType ==
'start' ) {
194 # Only move the start position if we haven't already found a start
195 # This means that START START END matches outer pair
196 if ( !$foundStart ) {
198 $inputPos = $tokenOffset + $tokenLength;
199 # Write out the non-matching section
200 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
201 $outputPos = $tokenOffset;
202 $contentPos = $inputPos;
205 # Move the input position past the *first character* of START,
206 # to protect against missing END when it overlaps with START
207 $inputPos = $tokenOffset + 1;
209 } elseif ( $tokenType ==
'end' ) {
212 $output .= $callback( [
213 substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
214 substr( $subject, $contentPos, $tokenOffset - $contentPos )
218 # Non-matching end, write it out
219 $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
221 $inputPos = $outputPos = $tokenOffset + $tokenLength;
223 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
226 if ( $outputPos < strlen( $subject ) ) {
227 $output .= substr( $subject, $outputPos );
249 $startDelim, $endDelim, $replace, $subject, $flags =
''
251 return self::delimiterReplaceCallback(
252 $startDelim, $endDelim,
253 static function ( array
$matches ) use ( $replace ) {
269 $placeholder =
"\x00";
272 $text = str_replace( $placeholder,
'', $text );
275 $cleaned = self::delimiterReplaceCallback(
277 static function ( array
$matches ) use ( $search, $placeholder ) {
278 return str_replace( $search, $placeholder,
$matches[0] );
284 $cleaned = str_replace( $search, $replace, $cleaned );
285 $text = str_replace( $placeholder, $search, $cleaned );
300 AtEase::suppressWarnings();
302 $isValid = preg_match( $string,
'' );
303 AtEase::restoreWarnings();
304 return $isValid !==
false;
315 $string = str_replace(
'\\',
'\\\\', $string );
316 return str_replace(
'$',
'\\$', $string );
326 public static function explode( $separator, $subject ) {
327 if ( substr_count( $subject, $separator ) > 1000 ) {
330 return new ArrayIterator(
explode( $separator, $subject ) );
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...