wp_check_invalid_utf8() WP 2.8.0

Click here to view original web page at wp-kama.ru

Проверяет наличие недопустимых UTF8 символов в строке.

Функция ничего не делает если опция get_option( 'blog_charset' ) не равна одному из utf8, utf-8, UTF8, UTF-8.

Для тестов есть специальный текст с недопустимыми UTF-8 символами. В нём примеры многих нарушений UTF-8, включая одиночные начальные байты, пропущенные байты продолжения, чрезмерно длинные последовательности и т.д.

Хуков нет.

Возвращает

Строку. Проверенный текст.

Использование

wp_check_invalid_utf8( $string, $strip );

Примеры

#1 Проверка работы функции

$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 );

Результат:

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) ""
}

Список изменений

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

	if ( 0 === strlen( $string ) ) {
		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 $string;
	}

	// 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 $string;
	}

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

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

	return '';
}

Cвязанные функции