wppaste
WordPress

_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
Internal compat function to mimic mb_substr().

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

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
7–50

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 82.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • regexregular expression over the whole inputpreg_match_all()called directly
  • hookthird-party callbacksapply_filters()one call below _mb_substr()
  • cacheobject cachewp_cache_get()one call below _mb_substr()
  • serializeserialisationmaybe_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.

WhenInstructionsCalls it makes
$str === null7none
$str !== null && $encoding !== null && !_is_utf8_charset()16–17_is_utf8_charset()
$str !== null && $encoding === null && !_is_utf8_charset()20–21get_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()36get_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()45get_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()50get_option(), _is_utf8_charset(), _wp_can_use_pcre_u(), array_pop(), preg_split(), array_merge(), array_pop(), array_slice()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev807–5082 fewer instructions than PHP 8.5
8.5827–508
8.4827–50812 fewer instructions than PHP 8.3
8.3947–538
8.2947–538
8.1947–538
7.4947–538

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

Used by · 1

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.

  1. 6.7.10
  2. 6.8.11
  3. 6.9.10
  4. 7.0.7
  5. 7.1.3

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.8.11 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.