wppaste
WordPress

_wp_utf8_codepoint_count( string $text, ?int $byte_offset = 0, ?int $max_byte_length = PHP_INT_MAX ): int

Since
6.9.0
Source
wp-includes/compat-utf8.php:341
Returns how many code points are found in the given UTF-8 string.

Description

Invalid spans of bytes count as a single code point according to the maximal subpart rule. This function is a fallback method for calling mb_strlen( $text, 'UTF-8' ).

When negative values are provided for the byte offsets or length, this will always report zero code points.

Example:

4 === _wp_utf8_codepoint_count( 'text' );

// Groups are 'test', "\x90" as '�', 'wp', "\xE2\x80" as '�', "\xC0" as '�', and 'test'.
13 === _wp_utf8_codepoint_count( "test\x90wp\xE2\x80\xC0test" );

Compatibility

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

Present in 3 of the 5 tracked releases, added in 6.9.0, and compiles on PHP 7.4 through 8.6-dev.

Parameters

$textstring
Count code points in this string.
$byte_offset?intoptional
Start counting after this many bytes in $text. Must be positive.Default: 0
$max_byte_length?intoptional
Stop counting after having scanned past this many bytes.
Default is to scan until the end of the string. Must be positive.Default: PHP_INT_MAX

Return value

int
How many code points were found.

Performance profile

How much work a call to _wp_utf8_codepoint_count() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
6–20

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
always6–20none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev376–204
8.5376–204
8.4376–2043 fewer instructions than PHP 8.3
8.3406–234
8.2406–234
8.1406–234
7.4406–234

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

  • _wp_scan_utf8()Finds spans of valid and invalid UTF-8 bytes in a given string.

Used by · 2

Source code

function _wp_utf8_codepoint_count( string $text, ?int $byte_offset = 0, ?int $max_byte_length = PHP_INT_MAX ): int {	if ( $byte_offset < 0 ) {		return 0;	} 	$count           = 0;	$at              = $byte_offset;	$end             = strlen( $text );	$invalid_length  = 0;	$max_byte_length = min( $end - $at, $max_byte_length ); 	while ( $at < $end && ( $at - $byte_offset ) < $max_byte_length ) {		$count += _wp_scan_utf8( $text, $at, $invalid_length, $max_byte_length - ( $at - $byte_offset ) );		$count += $invalid_length > 0 ? 1 : 0;		$at    += $invalid_length;	} 	return $count;}

Changelog

Introduced in 6.9.0. Unchanged from 6.9.7 through 7.1.0.

  1. 6.9.7
  2. 7.0.4
  3. 7.1.0

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

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/compat-utf8.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.