57 $subject, $nested =
false ) {
61 $encStart = preg_quote( $startDelim,
'!' );
62 $encEnd = preg_quote( $endDelim,
'!' );
63 $encSep = preg_quote( $separator,
'!' );
64 $len = strlen( $subject );
70 "!$encStart|$encEnd|$encSep!S", $subject, $m,
71 PREG_OFFSET_CAPTURE, $inputPos
76 $inputPos = $matchPos + strlen( $match );
77 if ( $match === $separator ) {
80 $subject, $lastPos, $matchPos - $lastPos
84 } elseif ( $match === $startDelim ) {
85 if ( $depth === 0 || $nested ) {
92 $exploded[] = substr( $subject, $lastPos );
95 return new ArrayIterator( $exploded );
116 $segments =
explode( $startDelim, $subject );
117 $output = array_shift( $segments );
118 foreach ( $segments as
$s ) {
119 $endDelimPos = strpos(
$s, $endDelim );
120 if ( $endDelimPos ===
false ) {
123 $output .= $replace . substr(
$s, $endDelimPos + strlen( $endDelim ) );
155 $subject, $flags =
''
162 $encStart = preg_quote( $startDelim,
'!' );
163 $encEnd = preg_quote( $endDelim,
'!' );
164 $strcmp = strpos( $flags,
'i' ) ===
false ?
'strcmp' :
'strcasecmp';
165 $endLength = strlen( $endDelim );
168 while ( $inputPos < strlen( $subject ) &&
169 preg_match(
"!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
171 $tokenOffset = $m[0][1];
172 if ( $m[1][0] !=
'' ) {
174 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
176 # An end match is present at the same location
178 $tokenLength = $endLength;
180 $tokenType =
'start';
181 $tokenLength = strlen( $m[0][0] );
183 } elseif ( $m[2][0] !=
'' ) {
185 $tokenLength = strlen( $m[0][0] );
187 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
190 if ( $tokenType ==
'start' ) {
191 # Only move the start position if we haven't already found a start
192 # This means that START START END matches outer pair
193 if ( !$foundStart ) {
195 $inputPos = $tokenOffset + $tokenLength;
196 # Write out the non-matching section
197 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
198 $outputPos = $tokenOffset;
199 $contentPos = $inputPos;
202 # Move the input position past the *first character* of START,
203 # to protect against missing END when it overlaps with START
204 $inputPos = $tokenOffset + 1;
206 } elseif ( $tokenType ==
'end' ) {
209 $output .= call_user_func( $callback, [
210 substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
211 substr( $subject, $contentPos, $tokenOffset - $contentPos )
215 # Non-matching end, write it out
216 $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
218 $inputPos = $outputPos = $tokenOffset + $tokenLength;
220 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
223 if ( $outputPos < strlen( $subject ) ) {
224 $output .= substr( $subject, $outputPos );
260 $placeholder =
"\x00";
263 $text = str_replace( $placeholder,
'', $text );
270 $items =
explode( $separator, $cleaned );
271 foreach ( $items as $i => $str ) {
272 $items[$i] = str_replace( $placeholder, $separator, $str );
287 $placeholder =
"\x00";
290 $text = str_replace( $placeholder,
'', $text );
297 $cleaned = str_replace( $search, $replace, $cleaned );
298 $text = str_replace( $placeholder, $search, $cleaned );
323 static function explode( $separator, $subject ) {
324 if ( substr_count( $subject, $separator ) > 1000 ) {
327 return new ArrayIterator(
explode( $separator, $subject ) );