Title: _wp_scrub_utf8_fallback
Published: February 24, 2026

---

# _wp_scrub_utf8_fallback( string $bytes ): string

## In this article

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

[ Back to top](https://developer.wordpress.org/reference/functions/_wp_scrub_utf8_fallback/?output_format=md#wp--skip-link--target)

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.

Fallback mechanism for replacing invalid spans of UTF-8 bytes.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/_wp_scrub_utf8_fallback/?output_format=md#description)󠁿

Example:

    ```php
    'Pi�a' === _wp_scrub_utf8_fallback( "Pi\xF1a" ); // “ñ” is 0xF1 in Windows-1252.
    ```

### 󠀁[See also](https://developer.wordpress.org/reference/functions/_wp_scrub_utf8_fallback/?output_format=md#see-also)󠁿

 * [wp_scrub_utf8()](https://developer.wordpress.org/reference/functions/wp_scrub_utf8/)

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

 `$bytes`stringrequired

UTF-8 encoded string which might contain spans of invalid bytes.

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

 string Input string with spans of invalid bytes swapped with the replacement character.

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

    ```php
    function _wp_scrub_utf8_fallback( string $bytes ): string {
    	$bytes_length   = strlen( $bytes );
    	$next_byte_at   = 0;
    	$was_at         = 0;
    	$invalid_length = 0;
    	$scrubbed       = '';

    	while ( $next_byte_at <= $bytes_length ) {
    		_wp_scan_utf8( $bytes, $next_byte_at, $invalid_length );

    		if ( $next_byte_at >= $bytes_length ) {
    			if ( 0 === $was_at ) {
    				return $bytes;
    			}

    			return $scrubbed . substr( $bytes, $was_at, $next_byte_at - $was_at - $invalid_length );
    		}

    		$scrubbed .= substr( $bytes, $was_at, $next_byte_at - $was_at );
    		$scrubbed .= "\u{FFFD}";

    		$next_byte_at += $invalid_length;
    		$was_at        = $next_byte_at;
    	}

    	return $scrubbed;
    }
    ```

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/_wp_scrub_utf8_fallback/?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_scrub_utf8_fallback/?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_scrub_utf8_fallback%2F)
before being able to contribute a note or feedback.