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

Description

Only supports UTF-8 and non-shifting single-byte encodings. For all other encodings expect the substrings to be misaligned. When the given encoding (or the blog_charset if none is provided) isn’t UTF-8 then the function returns the output of substr().

Compatibility

WordPress
since 3.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$strstring
The string to extract the substring from.
$startint
Character offset at which to start the substring extraction.
$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
Moderate

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

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
7–57

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

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
  • 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 · 11 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 && !_is_utf8_charset()17_is_utf8_charset()
$str !== null && !_is_utf8_charset()21get_option(), _is_utf8_charset()
$str !== null && _is_utf8_charset() && !$start && !$length && !isset($normalized_length)39–44_is_utf8_charset(), _wp_utf8_codepoint_span()
$str !== null && _is_utf8_charset() && !isset($normalized_length)41–48_is_utf8_charset(), _wp_utf8_codepoint_count(), _wp_utf8_codepoint_span()
$str !== null && _is_utf8_charset() && !$start && !$length && !isset($normalized_length)43–48get_option(), _is_utf8_charset(), _wp_utf8_codepoint_span()
$str !== null && _is_utf8_charset() && !$start && !$length && isset($normalized_length)44–49_is_utf8_charset(), _wp_utf8_codepoint_span(), _wp_utf8_codepoint_span()
$str !== null && _is_utf8_charset() && !isset($normalized_length)45–52get_option(), _is_utf8_charset(), _wp_utf8_codepoint_count(), _wp_utf8_codepoint_span()
$str !== null && _is_utf8_charset() && isset($normalized_length)46–53_is_utf8_charset(), _wp_utf8_codepoint_count(), _wp_utf8_codepoint_span(), _wp_utf8_codepoint_span()
$str !== null && _is_utf8_charset() && !$start && !$length && isset($normalized_length)48–53get_option(), _is_utf8_charset(), _wp_utf8_codepoint_span(), _wp_utf8_codepoint_span()
$str !== null && _is_utf8_charset() && isset($normalized_length)50–57get_option(), _is_utf8_charset(), _wp_utf8_codepoint_count(), _wp_utf8_codepoint_span(), _wp_utf8_codepoint_span()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev687–5493 fewer instructions than PHP 8.5
8.5717–579
8.4717–57919 fewer instructions than PHP 8.3
8.3907–689
8.2907–689
8.1907–6894 fewer instructions than PHP 7.4
7.4947–6810

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 · 4

  • _is_utf8_charset()Indicates if a given slug for a character set represents the UTF-8 text encoding.
  • get_option()Retrieves an option value based on an option name.
  • _wp_utf8_codepoint_count()Returns how many code points are found in the given UTF-8 string.
  • _wp_utf8_codepoint_span()Given a starting offset within a string and a maximum number of code points, return how many bytes are occupied by the span of characters.

Used by · 1

Source code

function _mb_substr( $str, $start, $length = null, $encoding = null ) {	if ( null === $str ) {		return '';	} 	// The solution below works only for UTF-8; treat all other encodings as byte streams.	if ( ! _is_utf8_charset( $encoding ?? get_option( 'blog_charset' ) ) ) {		$result = is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length ); 		/*		 * For an out-of-range start, substr() returns false on PHP < 8.0 but an		 * empty string on PHP >= 8.0. mb_substr() always returns an empty string,		 * so normalize to match its behavior across all supported PHP versions.		 */		return false === $result ? '' : $result;	} 	$total_length = ( $start < 0 || $length < 0 )		? _wp_utf8_codepoint_count( $str )		: 0; 	$normalized_start = $start < 0		? max( 0, $total_length + $start )		: $start; 	/*	 * The starting offset is provided as characters, which means this needs to	 * find how many bytes that many characters occupies at the start of the string.	 */	$starting_byte_offset = _wp_utf8_codepoint_span( $str, 0, $normalized_start ); 	$normalized_length = $length < 0		? max( 0, $total_length - $normalized_start + $length )		: $length; 	/*	 * This is the main step. It finds how many bytes the given length of code points	 * occupies in the input, starting at the byte offset calculated above.	 */	$byte_length = isset( $normalized_length )		? _wp_utf8_codepoint_span( $str, $starting_byte_offset, $normalized_length )		: ( strlen( $str ) - $starting_byte_offset ); 	// The result is a normal byte-level substring using the computed ranges.	return substr( $str, $starting_byte_offset, $byte_length );}

Changelog

Introduced in 3.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

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

About this page

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