MediaWiki  1.30.0
StringUtils Class Reference

A collection of static methods to play with strings. More...

Static Public Member Functions

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, which may optionally nest. More...
 
static delimiterReplace ( $startDelim, $endDelim, $replace, $subject, $flags='')
 Perform an operation equivalent to preg_replace() with flags. More...
 
static delimiterReplaceCallback ( $startDelim, $endDelim, $callback, $subject, $flags='')
 Perform an operation equivalent to preg_replace_callback() More...
 
static escapeRegexReplacement ( $string)
 Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter. More...
 
static explode ( $separator, $subject)
 Workalike for explode() with limited memory usage. More...
 
static explodeMarkup ( $separator, $text)
 More or less "markup-safe" explode() Ignores any instances of the separator inside <...> More...
 
static hungryDelimiterReplace ( $startDelim, $endDelim, $replace, $subject)
 Perform an operation equivalent to preg_replace() More...
 
static isUtf8 ( $value)
 Test whether a string is valid UTF-8. More...
 
static replaceMarkup ( $search, $replace, $text)
 More or less "markup-safe" str_replace() Ignores any instances of the separator inside <...> More...
 

Detailed Description

A collection of static methods to play with strings.

Definition at line 26 of file StringUtils.php.

Member Function Documentation

◆ delimiterExplode()

static StringUtils::delimiterExplode (   $startDelim,
  $endDelim,
  $separator,
  $subject,
  $nested = false 
)
static

Explode a string, but ignore any instances of the separator inside the given start and end delimiters, which may optionally nest.

The delimiters are literal strings, not regular expressions.

Parameters
string$startDelimStart delimiter
string$endDelimEnd delimiter
string$separatorSeparator string for the explode.
string$subjectSubject string to explode.
bool$nestedTrue iff the delimiters are allowed to nest.
Returns
ArrayIterator

Definition at line 68 of file StringUtils.php.

◆ delimiterReplace()

static StringUtils::delimiterReplace (   $startDelim,
  $endDelim,
  $replace,
  $subject,
  $flags = '' 
)
static

Perform an operation equivalent to preg_replace() with flags.

Matches this code:

preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject );
Parameters
string$startDelimStart delimiter regular expression
string$endDelimEnd delimiter regular expression
string$replaceReplacement string. May contain $1, which will be replaced by the text between the delimiters
string$subjectString to search
string$flagsRegular expression flags
Returns
string The string with the matches replaced

Definition at line 257 of file StringUtils.php.

References $flags, and delimiterReplaceCallback().

Referenced by CoreTagHooks\pre().

◆ delimiterReplaceCallback()

static StringUtils::delimiterReplaceCallback (   $startDelim,
  $endDelim,
  $callback,
  $subject,
  $flags = '' 
)
static

Perform an operation equivalent to preg_replace_callback()

Matches this code:

preg_replace_callback( "!$startDelim(.*)$endDelim!s$flags", $callback, $subject );

If the start delimiter ends with an initial substring of the end delimiter, e.g. in the case of C-style comments, the behavior differs from the model regex. In this implementation, the end must share no characters with the start, so e.g. /*\\/ is not considered to be both the start and end of a comment. /*\\/xy/*\\/ is considered to be a single comment with contents /xy/.

The implementation of delimiterReplaceCallback() is slower than hungryDelimiterReplace() but uses far less memory. The delimiters are literal strings, not regular expressions.

Parameters
string$startDelimStart delimiter
string$endDelimEnd delimiter
callable$callbackFunction to call on each match
string$subject
string$flagsRegular expression flags
Exceptions
InvalidArgumentException
Returns
string

Definition at line 166 of file StringUtils.php.

References $flags, and $output.

Referenced by delimiterReplace(), explodeMarkup(), and replaceMarkup().

◆ escapeRegexReplacement()

static StringUtils::escapeRegexReplacement (   $string)
static

Escape a string to make it suitable for inclusion in a preg_replace() replacement parameter.

Parameters
string$string
Returns
string

Definition at line 322 of file StringUtils.php.

Referenced by MovePageForm\doSubmit(), Linker\formatLinksInComment(), Title\moveSubpages(), MagicWord\replace(), SyncFileBackend\replaceNamePaths(), ExtParserFunctions\runReplace(), FileBackendMultiWrite\substPaths(), and FileBackendMultiWrite\unsubstPaths().

◆ explode()

static StringUtils::explode (   $separator,
  $subject 
)
static

Workalike for explode() with limited memory usage.

Parameters
string$separator
string$subject
Returns
ArrayIterator|ExplodeIterator

Definition at line 335 of file StringUtils.php.

Referenced by BlockLevelPass\execute(), explodeMarkup(), GenerateCollationData\generateFirstChars(), hungryDelimiterReplace(), and ConverterRule\parseFlags().

◆ explodeMarkup()

static StringUtils::explodeMarkup (   $separator,
  $text 
)
static

More or less "markup-safe" explode() Ignores any instances of the separator inside <...>

Parameters
string$separator
string$text
Returns
array

Definition at line 271 of file StringUtils.php.

References as, delimiterReplaceCallback(), and explode().

◆ hungryDelimiterReplace()

static StringUtils::hungryDelimiterReplace (   $startDelim,
  $endDelim,
  $replace,
  $subject 
)
static

Perform an operation equivalent to preg_replace()

Matches this code:

preg_replace( "!$startDelim(.*?)$endDelim!", $replace, $subject );

..except that it's worst-case O(N) instead of O(N^2). Compared to delimiterReplace(), this implementation is fast but memory-hungry and inflexible. The memory requirements are such that I don't recommend using it on anything but guaranteed small chunks of text.

Parameters
string$startDelim
string$endDelim
string$replace
string$subject
Returns
string

Definition at line 127 of file StringUtils.php.

References $output, $s, as, and explode().

◆ isUtf8()

static StringUtils::isUtf8 (   $value)
static

Test whether a string is valid UTF-8.

The function check for invalid byte sequences, overlong encoding but not for different normalisations.

Note
In MediaWiki 1.21, this function did not provide proper UTF-8 validation. In particular, the pure PHP code path did not in fact check for overlong forms. Beware of this when backporting code to that version of MediaWiki.
Since
1.21
Parameters
string$valueString to check
Returns
bool Whether the given $value is a valid UTF-8 encoded string

Definition at line 41 of file StringUtils.php.

References $value, and string.

Referenced by Language\checkTitleEncoding(), DatabaseOracle\doQuery(), and StringUtilsTest\testIsUtf8().

◆ replaceMarkup()

static StringUtils::replaceMarkup (   $search,
  $replace,
  $text 
)
static

More or less "markup-safe" str_replace() Ignores any instances of the separator inside <...>

Parameters
string$search
string$replace
string$text
Returns
string

Definition at line 298 of file StringUtils.php.

References delimiterReplaceCallback().


The documentation for this class was generated from the following file: