wp_check_invalid_utf8()WP 2.8.0

Checks for invalid UTF8 in a string.

No Hooks.

Return

String. The checked text.

Usage

wp_check_invalid_utf8( $text, $strip );
$text(string) (required)
The text which is to be checked.
$strip(true|false)
Whether to attempt to strip out invalid UTF8.
Default: false

Examples

0

#1 Checking the function

$examples = [
	'Valid ASCII'                               => "a",
	'Valid 2 Octet Sequence'                    => "\xc3\xb1",
	'Valid 3 Octet Sequence'                    => "\xe2\x82\xa1",
	'Valid 4 Octet Sequence'                    => "\xf0\x90\x8c\xbc",
	'Valid 5 Octet Sequence (but not Unicode!)' => "\xf8\xa1\xa1\xa1\xa1",
	'Valid 6 Octet Sequence (but not Unicode!)' => "\xfc\xa1\xa1\xa1\xa1\xa1",
	'Invalid 2 Octet Sequence'                  => "\xc3\x28",
	'Invalid Sequence Identifier'               => "\xa0\xa1",
	'Invalid 3 Octet Sequence (in 2nd Octet)'   => "\xe2\x28\xa1",
	'Invalid 3 Octet Sequence (in 3rd Octet)'   => "\xe2\x82\x28",
	'Invalid 4 Octet Sequence (in 2nd Octet)'   => "\xf0\x28\x8c\xbc",
	'Invalid 4 Octet Sequence (in 3rd Octet)'   => "\xf0\x90\x28\xbc",
	'Invalid 4 Octet Sequence (in 4th Octet)'   => "\xf0\x28\x8c\x28",
];

$result = [];

foreach ( $examples as $key => $value ) {
	$result[ $key ] = wp_check_invalid_utf8( $value );
}

var_dump( $result );

Result:

array(13) {
  ["Valid ASCII"]=>                               string(1) "a"
  ["Valid 2 Octet Sequence"]=>                    string(2) "ñ"
  ["Valid 3 Octet Sequence"]=>                    string(3) "₡"
  ["Valid 4 Octet Sequence"]=>                    string(4) "𐌼"
  ["Valid 5 Octet Sequence (but not Unicode!)"]=> string(0) ""
  ["Valid 6 Octet Sequence (but not Unicode!)"]=> string(0) ""
  ["Invalid 2 Octet Sequence"]=>                  string(0) ""
  ["Invalid Sequence Identifier"]=>               string(0) ""
  ["Invalid 3 Octet Sequence (in 2nd Octet)"]=>   string(0) ""
  ["Invalid 3 Octet Sequence (in 3rd Octet)"]=>   string(0) ""
  ["Invalid 4 Octet Sequence (in 2nd Octet)"]=>   string(0) ""
  ["Invalid 4 Octet Sequence (in 3rd Octet)"]=>   string(0) ""
  ["Invalid 4 Octet Sequence (in 4th Octet)"]=>   string(0) ""
}

Changelog

Since 2.8.0 Introduced.

wp_check_invalid_utf8() code WP 6.4.3

function wp_check_invalid_utf8( $text, $strip = false ) {
	$text = (string) $text;

	if ( 0 === strlen( $text ) ) {
		return '';
	}

	// Store the site charset as a static to avoid multiple calls to get_option().
	static $is_utf8 = null;
	if ( ! isset( $is_utf8 ) ) {
		$is_utf8 = in_array( get_option( 'blog_charset' ), array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true );
	}
	if ( ! $is_utf8 ) {
		return $text;
	}

	// Check for support for utf8 in the installed PCRE library once and store the result in a static.
	static $utf8_pcre = null;
	if ( ! isset( $utf8_pcre ) ) {
		// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
		$utf8_pcre = @preg_match( '/^./u', 'a' );
	}
	// We can't demand utf8 in the PCRE installation, so just return the string in those cases.
	if ( ! $utf8_pcre ) {
		return $text;
	}

	// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- preg_match fails when it encounters invalid UTF8 in $text.
	if ( 1 === @preg_match( '/^./us', $text ) ) {
		return $text;
	}

	// Attempt to strip the bad chars if requested (not recommended).
	if ( $strip && function_exists( 'iconv' ) ) {
		return iconv( 'utf-8', 'utf-8', $text );
	}

	return '';
}