5 * Perform an operation equivalent to
7 * preg_replace( "!$startDelim(.*?)$endDelim!", $replace, $subject );
9 * except that it's worst-case O(N) instead of O(N^2)
11 * Compared to delimiterReplace(), this implementation is fast but memory-
12 * hungry and inflexible. The memory requirements are such that I don't
13 * recommend using it on anything but guaranteed small chunks of text.
15 static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
16 $segments = explode( $startDelim, $subject );
17 $output = array_shift( $segments );
18 foreach ( $segments as $s ) {
19 $endDelimPos = strpos( $s, $endDelim );
20 if ( $endDelimPos === false ) {
21 $output .= $startDelim . $s;
23 $output .= $replace . substr( $s, $endDelimPos +
strlen( $endDelim ) );
30 * Perform an operation equivalent to
32 * preg_replace_callback( "!$startDelim(.*)$endDelim!s$flags", $callback, $subject )
34 * This implementation is slower than staticDelimiterReplace but uses far less
35 * memory and allows regular expression delimiters.
37 * @param string $flags Regular expression flags
39 static function delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags = '' ) {
45 while ( $inputPos < strlen( $subject ) &&
46 preg_match( "!($startDelim)|($endDelim)!$flags", $subject, $m, PREG_OFFSET_CAPTURE
, $inputPos ) )
48 if ( $m[1][0] != '' ) {
50 # Write out the non-matching section
51 $output .= substr( $subject, $outputPos, $m[1][1] - $outputPos );
53 $inputPos = $contentPos = $m[1][1] +
strlen( $m[1][0] );
54 $outputPos = $m[1][1];
55 } elseif ( $m[2][0] != '' ) {
59 $output .= call_user_func( $callback, array(
60 substr( $subject, $outputPos, $m[2][1] +
strlen( $m[2][0] ) - $outputPos ),
61 substr( $subject, $contentPos, $m[2][1] - $contentPos )
65 # Non-matching end, write it out
66 $output .= substr( $subject, $inputPos, $m[2][1] +
strlen( $m[2][0] ) - $outputPos );
68 $inputPos = $outputPos = $m[2][1] +
strlen( $m[2][0] );
70 throw new MWException( 'Invalid delimiter given to ' . __METHOD__
);
73 if ( $outputPos < strlen( $subject ) ) {
74 $output .= substr( $subject, $outputPos );
80 * Perform an operation equivalent to
82 * preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
84 * @param string $startDelim Start delimiter regular expression
85 * @param string $endDelim End delimiter regular expression
86 * @param string $replace Replacement string. May contain $1, which will be
87 * replaced by the text between the delimiters
88 * @param string $subject String to search
89 * @return string The string with the matches replaced
91 static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
92 $replacer = new RegexlikeReplacer( $replace );
93 return self
::delimiterReplaceCallback( $startDelim, $endDelim,
94 $replacer->cb(), $subject, $flags );
98 * More or less "markup-safe" explode()
99 * Ignores any instances of the separator inside <...>
100 * @param string $separator
101 * @param string $text
104 static function explodeMarkup( $separator, $text ) {
105 $placeholder = "\x00";
107 // Remove placeholder instances
108 $text = str_replace( $placeholder, '', $text );
110 // Replace instances of the separator inside HTML-like tags with the placeholder
111 $replacer = new DoubleReplacer( $separator, $placeholder );
112 $cleaned = StringUtils
::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
114 // Explode, then put the replaced separators back in
115 $items = explode( $separator, $cleaned );
116 foreach( $items as $i => $str ) {
117 $items[$i] = str_replace( $placeholder, $separator, $str );
124 * Escape a string to make it suitable for inclusion in a preg_replace()
125 * replacement parameter.
127 * @param string $string
130 static function escapeRegexReplacement( $string ) {
131 $string = str_replace( '\\', '\\\\', $string );
132 $string = str_replace( '$', '\\$', $string );
138 * Base class for "replacers", objects used in preg_replace_callback() and
139 * StringUtils::delimiterReplaceCallback()
143 return array( &$this, 'replace' );
148 * Class to replace regex matches with a string similar to that used in preg_replace()
150 class RegexlikeReplacer
extends Replacer
{
152 function __construct( $r ) {
156 function replace( $matches ) {
158 foreach ( $matches as $i => $match ) {
159 $pairs["\$$i"] = $match;
161 return strtr( $this->r
, $pairs );
167 * Class to perform secondary replacement within each replacement string
169 class DoubleReplacer
extends Replacer
{
170 function __construct( $from, $to, $index = 0 ) {
173 $this->index
= $index;
176 function replace( $matches ) {
177 return str_replace( $this->from
, $this->to
, $matches[$this->index
] );
182 * Class to perform replacement based on a simple hashtable lookup
184 class HashtableReplacer
extends Replacer
{
187 function __construct( $table, $index = 0 ) {
188 $this->table
= $table;
189 $this->index
= $index;
192 function replace( $matches ) {
193 return $this->table
[$matches[$this->index
]];
198 * Replacement array for FSS with fallback to strtr()
199 * Supports lazy initialisation of FSS resource
201 class ReplacementArray
{
202 /*mostly private*/ var $data = false;
203 /*mostly private*/ var $fss = false;
206 * Create an object with the specified replacement array
207 * The array should have the same form as the replacement array for strtr()
209 function __construct( $data = array() ) {
214 return array( 'data' );
217 function __wakeup() {
222 * Set the whole replacement array at once
224 function setArray( $data ) {
229 function getArray() {
234 * Set an element of the replacement array
236 function setPair( $from, $to ) {
237 $this->data
[$from] = $to;
241 function mergeArray( $data ) {
242 $this->data
= array_merge( $this->data
, $data );
246 function merge( $other ) {
247 $this->data
= array_merge( $this->data
, $other->data
);
251 function replace( $subject ) {
252 if ( function_exists( 'fss_prep_replace' ) ) {
253 wfProfileIn( __METHOD__
.'-fss' );
254 if ( $this->fss
=== false ) {
255 $this->fss
= fss_prep_replace( $this->data
);
257 $result = fss_exec_replace( $this->fss
, $subject );
258 wfProfileOut( __METHOD__
.'-fss' );
260 wfProfileIn( __METHOD__
.'-strtr' );
261 $result = strtr( $subject, $this->data
);
262 wfProfileOut( __METHOD__
.'-strtr' );