75 $segments =
explode( $startDelim, $subject );
76 $output = array_shift( $segments );
77 foreach ( $segments
as $s ) {
78 $endDelimPos = strpos(
$s, $endDelim );
79 if ( $endDelimPos ===
false ) {
82 $output .= $replace . substr(
$s, $endDelimPos + strlen( $endDelim ) );
120 $encStart = preg_quote( $startDelim,
'!' );
121 $encEnd = preg_quote( $endDelim,
'!' );
122 $strcmp = strpos(
$flags,
'i' ) ===
false ?
'strcmp' :
'strcasecmp';
123 $endLength = strlen( $endDelim );
126 while ( $inputPos < strlen( $subject ) &&
127 preg_match(
"!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
129 $tokenOffset = $m[0][1];
130 if ( $m[1][0] !=
'' ) {
132 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
134 # An end match is present at the same location
136 $tokenLength = $endLength;
138 $tokenType =
'start';
139 $tokenLength = strlen( $m[0][0] );
141 } elseif ( $m[2][0] !=
'' ) {
143 $tokenLength = strlen( $m[0][0] );
145 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
148 if ( $tokenType ==
'start' ) {
149 # Only move the start position if we haven't already found a start
150 # This means that START START END matches outer pair
151 if ( !$foundStart ) {
153 $inputPos = $tokenOffset + $tokenLength;
154 # Write out the non-matching section
155 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
156 $outputPos = $tokenOffset;
157 $contentPos = $inputPos;
160 # Move the input position past the *first character* of START,
161 # to protect against missing END when it overlaps with START
162 $inputPos = $tokenOffset + 1;
164 } elseif ( $tokenType ==
'end' ) {
167 $output .= call_user_func( $callback, [
168 substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
169 substr( $subject, $contentPos, $tokenOffset - $contentPos )
173 # Non-matching end, write it out
174 $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
176 $inputPos = $outputPos = $tokenOffset + $tokenLength;
178 throw new InvalidArgumentException(
'Invalid delimiter given to ' . __METHOD__ );
181 if ( $outputPos < strlen( $subject ) ) {
182 $output .= substr( $subject, $outputPos );
218 $placeholder =
"\x00";
221 $text = str_replace( $placeholder,
'', $text );
228 $items =
explode( $separator, $cleaned );
229 foreach ( $items
as $i => $str ) {
230 $items[$i] = str_replace( $placeholder, $separator, $str );
245 $placeholder =
"\x00";
248 $text = str_replace( $placeholder,
'', $text );
255 $cleaned = str_replace( $search, $replace, $cleaned );
256 $text = str_replace( $placeholder, $search, $cleaned );
281 static function explode( $separator, $subject ) {
282 if ( substr_count( $subject, $separator ) > 1000 ) {
285 return new ArrayIterator(
explode( $separator, $subject ) );