wp_is_valid_utf8( string $bytes ): bool
- Since
- 6.9.0
- Source
wp-includes/utf8.php:39
Description
Note that it’s unlikely for non-UTF-8 data to validate as UTF-8, but it is still possible. Many texts are simultaneously valid UTF-8, valid US-ASCII, and valid ISO-8859-1 (latin1).
Example:
true === wp_is_valid_utf8( '' );
true === wp_is_valid_utf8( 'just a test' );
true === wp_is_valid_utf8( "\xE2\x9C\x8F" ); // Pencil, U+270F.
true === wp_is_valid_utf8( "\u{270F}" ); // Pencil, U+270F.
true === wp_is_valid_utf8( '✏' ); // Pencil, U+270F.
false === wp_is_valid_utf8( "just xC0 test" ); // Invalid bytes.
false === wp_is_valid_utf8( "\xE2\x9C" ); // Invalid/incomplete sequences.
false === wp_is_valid_utf8( "\xC1\xBF" ); // Overlong sequences.
false === wp_is_valid_utf8( "\xED\xB0\x80" ); // Surrogate halves.
false === wp_is_valid_utf8( "B\xFCch" ); // ISO-8859-1 high-bytes.
// E.g. The “ü” in ISO-8859-1 is a single byte 0xFC,
// but in UTF-8 is the two-byte sequence 0xC3 0xBC. A “valid” string consists of “well-formed UTF-8 code unit sequence[s],” meaning that the bytes conform to the UTF-8 encoding scheme, all characters use the minimal byte sequence required by UTF-8, and that no sequence encodes a UTF-16 surrogate code point or any character above the representable range.
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
$bytesstring- String which might contain text encoded as UTF-8.
Return value
bool- Whether the provided bytes can decode as valid UTF-8.
Performance profile
How much work a call to wp_is_valid_utf8() 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
- Trivial
- Scaling
- Constant
- Instructions
- 6
- Plugin surface
- None
- Called by
- 6
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 6.
Nothing here hands control to plugin code.
6 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_is_valid_utf8() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6 | mb_check_encoding() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 7 | 7 | 0 | 1 more instruction than PHP 8.5 |
| 8.5 | 6 | 6 | 0 | |
| 8.4 | 6 | 6 | 0 | |
| 8.3 | 6 | 6 | 0 | |
| 8.2 | 6 | 6 | 0 | |
| 8.1 | 6 | 6 | 0 | |
| 7.4 | 6 | 6 | 0 |
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.
Used by · 6
- remove_accents()Converts all accent characters to ASCII characters.
- sanitize_file_name()Sanitizes a filename, replacing whitespace with dashes.
- sanitize_title_with_dashes()Sanitizes a title, replacing whitespace and a few other characters with dashes.
- wp_check_invalid_utf8()Checks for invalid UTF8 in a string.
- wp_read_image_metadata()Gets extended image metadata, exif or iptc as available.
- wxr_cdata()Wraps given string in XML CDATA tag.
Source code
function wp_is_valid_utf8( string $bytes ): bool { return mb_check_encoding( $bytes, 'UTF-8' ); }Changelog
Introduced in 6.9.0. Unchanged from 6.9.7 through 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/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.