_mb_substr( string $str, int $start, int|null $length = null, string|null $encoding = null ): string
- Since
- 3.2.0
- Source
wp-includes/compat.php:123
Description
Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence. The behavior of this function for invalid inputs is undefined.
Compatibility
- WordPress
- since 3.2.0
- PHP
- 7.4–8.6-dev
- 6.7.10
- 6.8.11
- 6.9.10
- 7.0.7
- 7.1.3
Present in every tracked release (6.7.10 to 7.1.3), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$strstring- The string to extract the substring from.
$startint- Position to being extraction from in
$str. $lengthint|nulloptional- Maximum number of characters to extract from
$str.
Default null.Default:null $encodingstring|nulloptional- Character encoding to use. Default null.Default:
null
Return value
string- Extracted substring.
Performance profile
How much work a call to _mb_substr() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 7–50
- Plugin surface
- None
- Called by
- 1
Reads stored settings via get_option(), cached per request but not free on a cold cache.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - regexregular expression over the whole input
preg_match_all()called directly - hookthird-party callbacks
apply_filters()one call below _mb_substr() - cacheobject cache
wp_cache_get()one call below _mb_substr() - serializeserialisation
maybe_unserialize()one call below _mb_substr()
Further down the call graph this can also reach query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _mb_substr() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$str === null | 7 | none |
$str !== null && $encoding !== null && !_is_utf8_charset() | 16–17 | _is_utf8_charset() |
$str !== null && $encoding === null && !_is_utf8_charset() | 20–21 | get_option(), _is_utf8_charset() |
$str !== null && $encoding !== null && _is_utf8_charset() && _wp_can_use_pcre_u() | 32 | _is_utf8_charset(), _wp_can_use_pcre_u(), preg_match_all(), array_slice() |
$str !== null && $encoding === null && _is_utf8_charset() && _wp_can_use_pcre_u() | 36 | get_option(), _is_utf8_charset(), _wp_can_use_pcre_u(), preg_match_all(), array_slice() |
$str !== null && $encoding !== null && _is_utf8_charset() && !_wp_can_use_pcre_u() | 41 | _is_utf8_charset(), _wp_can_use_pcre_u(), array_pop(), preg_split(), array_merge(), array_slice() |
$str !== null && $encoding === null && _is_utf8_charset() && !_wp_can_use_pcre_u() | 45 | get_option(), _is_utf8_charset(), _wp_can_use_pcre_u(), array_pop(), preg_split(), array_merge(), array_slice() |
$str !== null && $encoding !== null && _is_utf8_charset() && !_wp_can_use_pcre_u() | 46 | _is_utf8_charset(), _wp_can_use_pcre_u(), array_pop(), preg_split(), array_merge(), array_pop(), array_slice() |
$str !== null && $encoding === null && _is_utf8_charset() && !_wp_can_use_pcre_u() | 50 | get_option(), _is_utf8_charset(), _wp_can_use_pcre_u(), array_pop(), preg_split(), array_merge(), array_pop(), array_slice() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 80 | 7–50 | 8 | 2 fewer instructions than PHP 8.5 |
| 8.5 | 82 | 7–50 | 8 | |
| 8.4 | 82 | 7–50 | 8 | 12 fewer instructions than PHP 8.3 |
| 8.3 | 94 | 7–53 | 8 | |
| 8.2 | 94 | 7–53 | 8 | |
| 8.1 | 94 | 7–53 | 8 | |
| 7.4 | 94 | 7–53 | 8 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- get_option()Retrieves an option value based on an option name.
- _is_utf8_charset()Indicates if a given slug for a character set represents the UTF-8 text encoding.
- _wp_can_use_pcre_u()Returns whether PCRE/u (PCRE_UTF8 modifier) is available for use.
Used by · 1
- mb_substr()Compat function to mimic mb_substr().
Source code
function _mb_substr( $str, $start, $length = null, $encoding = null ) { if ( null === $str ) { return ''; } if ( null === $encoding ) { $encoding = get_option( 'blog_charset' ); } /* * The solution below works only for UTF-8, so in case of a different * charset just use built-in substr(). */ if ( ! _is_utf8_charset( $encoding ) ) { return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length ); } if ( _wp_can_use_pcre_u() ) { // Use the regex unicode support to separate the UTF-8 characters into an array. preg_match_all( '/./us', $str, $match ); $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length ); return implode( '', $chars ); } $regex = '/( [\x00-\x7F] # single-byte sequences 0xxxxxxx | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 | [\xE1-\xEC][\x80-\xBF]{2} | \xED[\x80-\x9F][\x80-\xBF] | [\xEE-\xEF][\x80-\xBF]{2} | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 | [\xF1-\xF3][\x80-\xBF]{3} | \xF4[\x80-\x8F][\x80-\xBF]{2} )/x'; // Start with 1 element instead of 0 since the first thing we do is pop. $chars = array( '' ); do { // We had some string left over from the last round, but we counted it in that last round. array_pop( $chars ); /* * Split by UTF-8 character, limit to 1000 characters (last array element will contain * the rest of the string). */ $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); $chars = array_merge( $chars, $pieces ); // If there's anything left over, repeat the loop. } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); return implode( '', array_slice( $chars, $start, $length ) );}Changelog
Introduced in 3.2.0. Unchanged from 6.7.10 through 7.1.3.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.10 tag, from
src/wp-includes/compat.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.