utf8_uri_encode( string $utf8_string, int $length = 0, bool $encode_ascii_characters = false ): string
- Since
- 1.5.0, 5.8.3
- Source
wp-includes/formatting.php:1160
Compatibility
- WordPress
- since 5.8.3
- 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
$utf8_stringstring- String to encode.
$lengthintoptional- Max length of the string.Default:
0 $encode_ascii_charactersbooloptional- Whether to encode ascii characters such as < " 'Default:
false
Return value
string- String with Unicode encoded for URI.
Performance profile
How much work a call to utf8_uri_encode() 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
- Scaling
- Scales with input
- Instructions
- 17–39
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 81.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost utf8_uri_encode() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$i | 17 | mbstring_binary_safe_encoding(), reset_mbstring_encoding() |
$i && !$value && $length | 33–39 | mbstring_binary_safe_encoding(), reset_mbstring_encoding(), ord() |
$i && $value && $length | 35 | mbstring_binary_safe_encoding(), reset_mbstring_encoding(), ord(), chr() |
$i && $value && $length | 39 | mbstring_binary_safe_encoding(), reset_mbstring_encoding(), ord(), chr(), rawurlencode() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 80 | 17–39 | 12 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 81 | 17–39 | 12 | |
| 8.4 | 81 | 17–39 | 12 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 83 | 17–39 | 12 | |
| 8.2 | 83 | 17–39 | 12 | |
| 8.1 | 83 | 17–39 | 12 | 2 more instructions than PHP 7.4 |
| 7.4 | 81 | 17–39 | 12 |
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 · 2
- mbstring_binary_safe_encoding()Sets the mbstring internal encoding to a binary safe encoding when func_overload is enabled.
- reset_mbstring_encoding()Resets the mbstring internal encoding to a users previously set encoding.
Used by · 2
- _truncate_post_slug()Truncates a post slug.
- sanitize_title_with_dashes()Sanitizes a title, replacing whitespace and a few other characters with dashes.
Source code
function utf8_uri_encode( $utf8_string, $length = 0, $encode_ascii_characters = false ) { $unicode = ''; $values = array(); $num_octets = 1; $unicode_length = 0; mbstring_binary_safe_encoding(); $string_length = strlen( $utf8_string ); reset_mbstring_encoding(); for ( $i = 0; $i < $string_length; $i++ ) { $value = ord( $utf8_string[ $i ] ); if ( $value < 128 ) { $char = chr( $value ); $encoded_char = $encode_ascii_characters ? rawurlencode( $char ) : $char; $encoded_char_length = strlen( $encoded_char ); if ( $length && ( $unicode_length + $encoded_char_length ) > $length ) { break; } $unicode .= $encoded_char; $unicode_length += $encoded_char_length; } else { if ( 0 === count( $values ) ) { if ( $value < 224 ) { $num_octets = 2; } elseif ( $value < 240 ) { $num_octets = 3; } else { $num_octets = 4; } } $values[] = $value; if ( $length && ( $unicode_length + ( $num_octets * 3 ) ) > $length ) { break; } if ( count( $values ) === $num_octets ) { for ( $j = 0; $j < $num_octets; $j++ ) { $unicode .= '%' . dechex( $values[ $j ] ); } $unicode_length += $num_octets * 3; $values = array(); $num_octets = 1; } } } return $unicode;}Changelog
Introduced in 1.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
encode_ascii_characters parameter.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/formatting.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.