cb(), $subject, $flags ); } /** * More or less "markup-safe" explode() * Ignores any instances of the separator inside `<...>` * @param string $separator * @param string $text * @return array */ static function explodeMarkup( $separator, $text ) { $placeholder = "\x00"; // Remove placeholder instances $text = str_replace( $placeholder, '', $text ); // Replace instances of the separator inside HTML-like tags with the placeholder $replacer = new DoubleReplacer( $separator, $placeholder ); $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text ); // Explode, then put the replaced separators back in $items = explode( $separator, $cleaned ); foreach ( $items as $i => $str ) { $items[$i] = str_replace( $placeholder, $separator, $str ); } return $items; } /** * More or less "markup-safe" str_replace() * Ignores any instances of the separator inside `<...>` * @param string $search * @param string $replace * @param string $text * @return string */ static function replaceMarkup( $search, $replace, $text ) { $placeholder = "\x00"; // Remove placeholder instances $text = str_replace( $placeholder, '', $text ); // Replace instances of the separator inside HTML-like tags with the placeholder $replacer = new DoubleReplacer( $search, $placeholder ); $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text ); // Explode, then put the replaced separators back in $cleaned = str_replace( $search, $replace, $cleaned ); $text = str_replace( $placeholder, $search, $cleaned ); return $text; } /** * Escape a string to make it suitable for inclusion in a preg_replace() * replacement parameter. * * @param string $string * @return string */ static function escapeRegexReplacement( $string ) { $string = str_replace( '\\', '\\\\', $string ); $string = str_replace( '$', '\\$', $string ); return $string; } /** * Workalike for explode() with limited memory usage. * * @param string $separator * @param string $subject * @return ArrayIterator|ExplodeIterator */ static function explode( $separator, $subject ) { if ( substr_count( $subject, $separator ) > 1000 ) { return new ExplodeIterator( $separator, $subject ); } else { return new ArrayIterator( explode( $separator, $subject ) ); } } }