MediaWiki  REL1_22
StringUtils Class Reference

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

List of all members.

Static Public Member Functions

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

Detailed Description

A collection of static methods to play with strings.

Definition at line 26 of file StringUtils.php.


Member Function Documentation

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

Perform an operation equivalent to.

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$subjectto search
string$flagsregular expression flags
Returns:
String: The string with the matches replaced

Definition at line 252 of file StringUtils.php.

References $flags, and delimiterReplaceCallback().

Referenced by Sanitizer\normalizeCss(), CoreTagHooks\pre(), and Sanitizer\stripAllTags().

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

Perform an operation equivalent to.

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

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

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/.

Parameters:
string$startDelimstart delimiter
string$endDelimend delimiter
$callbackCallback: function to call on each match
$subjectString
string$flagsregular expression flags
Exceptions:
MWException
Returns:
string

Definition at line 167 of file StringUtils.php.

References $flags, $output, and array().

Referenced by delimiterReplace(), and explodeMarkup().

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 291 of file StringUtils.php.

Referenced by MovePageForm\doSubmit(), Linker\formatLinksInCommentCallback(), Title\moveSubpages(), MagicWord\replace(), and SyncFileBackend\replaceNamePaths().

static StringUtils::explode ( separator,
subject 
) [static]

Workalike for explode() with limited memory usage.

Returns an Iterator

Parameters:
string$separator
string$subject
Returns:
ArrayIterator|ExplodeIterator

Definition at line 304 of file StringUtils.php.

Referenced by CLDRPluralRuleEvaluator\evaluateCompiled(), explodeMarkup(), GenerateCollationData\generateFirstChars(), ConfEditorParseError\highlight(), and hungryDelimiterReplace().

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 265 of file StringUtils.php.

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

static StringUtils::hungryDelimiterReplace ( startDelim,
endDelim,
replace,
subject 
) [static]

Perform an operation equivalent to.

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:
$startDelim
$endDelim
$replace
$subject
Returns:
string

Definition at line 131 of file StringUtils.php.

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

static StringUtils::isUtf8 ( value,
disableMbstring = false 
) [static]

Test whether a string is valid UTF-8.

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

This relies internally on the mbstring function mb_check_encoding() hardcoded to check against UTF-8. Whenever the function is not available we fallback to a pure PHP implementation. Setting $disableMbstring to true will skip the use of mb_check_encoding, this is mostly intended for unit testing our internal implementation.

Since:
1.21
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.
Parameters:
string$valueString to check
boolean$disableMbstringWhether to use the pure PHP implementation instead of trying mb_check_encoding. Intended for unit testing. Default: false
Returns:
boolean Whether the given $value is a valid UTF-8 encoded string

Definition at line 52 of file StringUtils.php.

References $value, array(), and as.

Referenced by DatabaseOracle\doQuery(), StringUtilsTest\testIsUtf8WithMbstring(), and StringUtilsTest\testIsUtf8WithPhpFallbackImplementation().


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