函数文档

_wp_is_valid_utf8_fallback()

💡 云策文档标注

概述

_wp_is_valid_utf8_fallback() 是 WordPress 中用于安全验证 UTF-8 字节的回退机制函数。它检查提供的字符串是否可以作为有效的 UTF-8 解码,通常作为 wp_is_valid_utf8() 的备用方案。

关键要点

  • 函数用于验证字符串是否包含有效的 UTF-8 编码字节
  • 接受一个必需的字符串参数 $bytes,返回布尔值表示有效性
  • 内部调用 _wp_scan_utf8() 函数来扫描字符串中的有效和无效字节
  • 在 WordPress 6.9.0 版本中引入

代码示例

function _wp_is_valid_utf8_fallback( string $bytes ): bool {
    $bytes_length = strlen( $bytes );
    if ( 0 === $bytes_length ) {
        return true;
    }

    $next_byte_at   = 0;
    $invalid_length = 0;

    _wp_scan_utf8( $bytes, $next_byte_at, $invalid_length );

    return $bytes_length === $next_byte_at && 0 === $invalid_length;
}

📄 原文内容

Fallback mechanism for safely validating UTF-8 bytes.

Description

See also

Parameters

$bytesstringrequired
String which might contain text encoded as UTF-8.

Return

bool Whether the provided bytes can decode as valid UTF-8.

Source

function _wp_is_valid_utf8_fallback( string $bytes ): bool {
	$bytes_length = strlen( $bytes );
	if ( 0 === $bytes_length ) {
		return true;
	}

	$next_byte_at   = 0;
	$invalid_length = 0;

	_wp_scan_utf8( $bytes, $next_byte_at, $invalid_length );

	return $bytes_length === $next_byte_at && 0 === $invalid_length;
}

Changelog

Version Description
6.9.0 Introduced.