On this page
seems_utf8( string $str ): bool
Checks to see if a string is utf8 encoded.
Description
NOTE: This function checks for 5-Byte sequences, UTF8 has Bytes Sequences with a maximum length of 4.
Parameters
$strstring Required-
The string to be checked
Return
bool True if $str fits a UTF-8 model, false otherwise.
Source
File: wp-includes/formatting.php. View all references
function seems_utf8( $str ) {
mbstring_binary_safe_encoding();
$length = strlen( $str );
reset_mbstring_encoding();
for ( $i = 0; $i < $length; $i++ ) {
$c = ord( $str[ $i ] );
if ( $c < 0x80 ) {
$n = 0; // 0bbbbbbb
} elseif ( ( $c & 0xE0 ) == 0xC0 ) {
$n = 1; // 110bbbbb
} elseif ( ( $c & 0xF0 ) == 0xE0 ) {
$n = 2; // 1110bbbb
} elseif ( ( $c & 0xF8 ) == 0xF0 ) {
$n = 3; // 11110bbb
} elseif ( ( $c & 0xFC ) == 0xF8 ) {
$n = 4; // 111110bb
} elseif ( ( $c & 0xFE ) == 0xFC ) {
$n = 5; // 1111110b
} else {
return false; // Does not match any model.
}
for ( $j = 0; $j < $n; $j++ ) { // n bytes matching 10bbbbbb follow ?
if ( ( ++$i == $length ) || ( ( ord( $str[ $i ] ) & 0xC0 ) != 0x80 ) ) {
return false;
}
}
}
return true;
}
Related
Uses
| Uses | Description |
|---|---|
| mbstring_binary_safe_encoding() wp-includes/functions.php | Sets the mbstring internal encoding to a binary safe encoding when func_overload is enabled. |
| reset_mbstring_encoding() wp-includes/functions.php | Resets the mbstring internal encoding to a users previously set encoding. |
Used By
| Used By | Description |
|---|---|
| wxr_cdata() wp-admin/includes/export.php | Wraps given string in XML CDATA tag. |
| wp_read_image_metadata() wp-admin/includes/image.php | Gets extended image metadata, exif or iptc as available. |
| sanitize_file_name() wp-includes/formatting.php | Sanitizes a filename, replacing whitespace with dashes. |
| sanitize_title_with_dashes() wp-includes/formatting.php | Sanitizes a title, replacing whitespace and a few other characters with dashes. |
| remove_accents() wp-includes/formatting.php | Converts all accent characters to ASCII characters. |
Changelog
| Version | Description |
|---|---|
| 1.2.1 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/seems_utf8