WordPress at a glance

seems_utf8() WP 1.2.1

Checks to see if a string is UTF-8 encoded.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.02 sec (speed of light) | PHP 7.2.16, WP 5.2.1

No Hooks.

Return

true|false. True if $str fits a UTF-8 model, false otherwise.

Usage

seems_utf8( $str );
$str(string) (required)
The string to be checked

Examples

#1 Work demonstration

$str = 'Text in utf8 encoding';

if( seems_utf8( $str ) )
	echo 'UTF-8 Encoding';
else
	echo 'Not UTF-8 ';

Changelog

Since 1.2.1 Introduced.

Code of seems_utf8() WP 5.7.2

<?php
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;
}
No comments
    Log In