wppaste
WordPress

_wp_utf8_decode_fallback( string $utf8_text ): string

Since
6.9.0
Source
wp-includes/compat-utf8.php:487
Converts a string from UTF-8 to ISO-8859-1, maintaining backwards compatibility with the deprecated function from the PHP standard library.

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

$utf8_textstring
Text treated as UTF-8 bytes.

Return value

string
Text converted into ISO-8859-1.

Performance profile

How much work a call to _wp_utf8_decode_fallback() 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
13–38

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

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 one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
!$at13–15none
$at && !$ascii_byte_count && $found !== 1 && !$invalid_length36–38strspn(), _wp_scan_utf8()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev7713–388
8.57713–388
8.47713–3886 fewer instructions than PHP 8.3
8.38313–418
8.28313–418
8.18313–418
7.48313–418

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

Source code

function _wp_utf8_decode_fallback( $utf8_text ) {	$utf8_text       = (string) $utf8_text;	$at              = 0;	$was_at          = 0;	$end             = strlen( $utf8_text );	$iso_8859_1_text = ''; 	while ( $at < $end ) {		// US-ASCII bytes are identical in ISO-8859-1 and UTF-8. These are 0x00–0x7F.		$ascii_byte_count = strspn(			$utf8_text,			"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" .			"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" .			" !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\x7f",			$at		); 		if ( $ascii_byte_count > 0 ) {			$at += $ascii_byte_count;			continue;		} 		$next_at        = $at;		$invalid_length = 0;		$found          = _wp_scan_utf8( $utf8_text, $next_at, $invalid_length, null, 1 );		$span_length    = $next_at - $at;		$next_byte      = '?'; 		if ( 1 !== $found ) {			if ( $invalid_length > 0 ) {				$next_byte = '';				goto flush_sub_part;			} 			break;		} 		// All convertible code points are two-bytes long.		$byte1 = ord( $utf8_text[ $at ] );		if ( 0xC0 !== ( $byte1 & 0xE0 ) ) {			goto flush_sub_part;		} 		// All convertible code points are not greater than U+FF.		$byte2      = ord( $utf8_text[ $at + 1 ] );		$code_point = ( ( $byte1 & 0x1F ) << 6 ) | ( ( $byte2 & 0x3F ) );		if ( $code_point > 0xFF ) {			goto flush_sub_part;		} 		$next_byte = chr( $code_point ); 		flush_sub_part:		$iso_8859_1_text .= substr( $utf8_text, $was_at, $at - $was_at );		$iso_8859_1_text .= $next_byte;		$at              += $span_length;		$was_at           = $at; 		if ( $invalid_length > 0 ) {			$iso_8859_1_text .= '?';			$at              += $invalid_length;			$was_at           = $at;		}	} 	if ( 0 === $was_at ) {		return $utf8_text;	} 	$iso_8859_1_text .= substr( $utf8_text, $was_at );	return $iso_8859_1_text;}

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.