wppaste
WordPress

_mb_strlen( string $str, string|null $encoding = null ): int

Since
4.2.0
Source
wp-includes/compat.php:212
Internal compat function to mimic mb_strlen().

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 4.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 retrieve the character length from.
$encodingstring|nulloptional
Character encoding to use. Default null.Default: null

Return value

int
String length of $str.

Performance profile

How much work a call to _mb_strlen() 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
10–32

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

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_strlen()
  • cacheobject cachewp_cache_get()one call below _mb_strlen()
  • serializeserialisationmaybe_unserialize()one call below _mb_strlen()

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 · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _mb_strlen() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$encoding !== null && !_is_utf8_charset()10_is_utf8_charset()
$encoding === null && !_is_utf8_charset()14get_option(), _is_utf8_charset()
$encoding !== null && _is_utf8_charset() && _wp_can_use_pcre_u()19_is_utf8_charset(), _wp_can_use_pcre_u(), preg_match_all()
$encoding === null && _is_utf8_charset() && _wp_can_use_pcre_u()23get_option(), _is_utf8_charset(), _wp_can_use_pcre_u(), preg_match_all()
$encoding !== null && _is_utf8_charset() && !_wp_can_use_pcre_u()28_is_utf8_charset(), _wp_can_use_pcre_u(), preg_split(), array_pop()
$encoding === null && _is_utf8_charset() && !_wp_can_use_pcre_u()32get_option(), _is_utf8_charset(), _wp_can_use_pcre_u(), preg_split(), array_pop()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 42 instructions, 10–32 executed per call, 4 branches. The work does not change between versions.

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_strlen( $str, $encoding = null ) {	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 strlen().	 */	if ( ! _is_utf8_charset( $encoding ) ) {		return strlen( $str );	} 	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 );		return count( $match[0] );	} 	$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 at 1 instead of 0 since the first thing we do is decrement.	$count = 1; 	do {		// We had some string left over from the last round, but we counted it in that last round.		--$count; 		/*		 * 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 ); 		// Increment.		$count += count( $pieces ); 		// If there's anything left over, repeat the loop.	} while ( $str = array_pop( $pieces ) ); 	// Fencepost: preg_split() always returns one extra item in the array.	return --$count;}

Changelog

Introduced in 4.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.