Title: _wp_utf8_codepoint_span
Published: February 24, 2026
Last modified: August 20, 2026

---

# _wp_utf8_codepoint_span( string $text, int $byte_offset, int $max_code_points, ?int $found_code_points ): int

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#changelog)

This function’s access is marked private. This means it is not intended for use 
by plugin or theme developers, only by core. It is listed here for completeness.

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.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#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 `strlen( mb_substr( substr(
$text, $at ), 0, $max_code_points ) )`.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#parameters)󠁿

 `$text`stringrequired

Count bytes of span in this text.

`$byte_offset`intrequired

Start counting at this byte offset.

`$max_code_points`intrequired

Stop counting after this many code points have been seen, or at the end of the string.

`$found_code_points`?intoptional

Will be set to number of found code points in span, as this might be smaller than
the maximum count if the string is not long enough.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#return)󠁿

 int Number of bytes spanned by the code points.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#source)󠁿

    ```php
    function _wp_utf8_codepoint_span( string $text, int $byte_offset, int $max_code_points, ?int &$found_code_points = 0 ): int {
    	$was_at            = $byte_offset;
    	$invalid_length    = 0;
    	$end               = strlen( $text );
    	$found_code_points = 0;

    	while ( $byte_offset < $end && $found_code_points < $max_code_points ) {
    		$needed      = $max_code_points - $found_code_points;
    		$chunk_count = _wp_scan_utf8( $text, $byte_offset, $invalid_length, null, $needed );

    		$found_code_points += $chunk_count;

    		// Invalid spans only convey one code point count regardless of how long they are.
    		if ( 0 !== $invalid_length && $found_code_points < $max_code_points ) {
    			++$found_code_points;
    			$byte_offset += $invalid_length;
    		}
    	}

    	return $byte_offset - $was_at;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/compat-utf8.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/compat-utf8.php#L381)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/compat-utf8.php#L381-L401)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#related)󠁿

| Uses | Description | 
| [_wp_scan_utf8()](https://developer.wordpress.org/reference/functions/_wp_scan_utf8/)`wp-includes/compat-utf8.php` |

Finds spans of valid and invalid UTF-8 bytes in a given string.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/_wp_utf8_codepoint_span/?output_format=md#changelog)󠁿

| Version | Description | 
| [6.9.0](https://developer.wordpress.org/reference/since/6.9.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2F_wp_utf8_codepoint_span%2F)
before being able to contribute a note or feedback.