函数文档

is_utf8_charset()

💡 云策文档标注

概述

is_utf8_charset() 函数用于判断给定的字符集 slug 是否代表 UTF-8 文本编码。如果未提供参数,则检查当前博客的字符集设置。

关键要点

  • 函数通过不区分大小写匹配 "UTF-8"(带或不带连字符)来确定字符集是否为 UTF-8。
  • 参数 $blog_charset 可选,默认从 "blog_charset" 选项推断,返回布尔值。
  • 仅字符串参数有效,非字符串类型(如数组)会返回 false。

代码示例

true  === is_utf8_charset( 'UTF-8' );
true  === is_utf8_charset( 'utf8' );
false === is_utf8_charset( 'latin1' );
false === is_utf8_charset( 'UTF 8' );

// Only strings match.
false === is_utf8_charset( [ 'charset' => 'utf-8' ] );

// Without a given charset, it depends on the site option "blog_charset".
$is_utf8 = is_utf8_charset();

注意事项

  • 函数是 _is_utf8_charset 的包装器,自 WordPress 6.6.0 引入,6.6.1 版本更新。
  • 相关函数包括 get_option()、wp_check_invalid_utf8() 和 _canonical_charset(),用于字符集处理和验证。

📄 原文内容

Indicates if a given slug for a character set represents the UTF-8 text encoding. If not provided, examines the current blog’s charset.

Description

A charset is considered to represent UTF-8 if it is a case-insensitive match of “UTF-8” with or without the hyphen.

Example:

true  === is_utf8_charset( 'UTF-8' );
true  === is_utf8_charset( 'utf8' );
false === is_utf8_charset( 'latin1' );
false === is_utf8_charset( 'UTF 8' );

// Only strings match.
false === is_utf8_charset( [ 'charset' => 'utf-8' ] );

// Without a given charset, it depends on the site option "blog_charset".
$is_utf8 = is_utf8_charset();

See also

  • _is_utf8_charset

Parameters

$blog_charsetstring|nulloptional
Slug representing a text character encoding, or “charset”.
E.g. “UTF-8”, “Windows-1252”, “ISO-8859-1”, “SJIS”.
Default value is to infer from “blog_charset” option.

Default:null

Return

bool Whether the slug represents the UTF-8 encoding.

Source

function is_utf8_charset( $blog_charset = null ) {
	return _is_utf8_charset( $blog_charset ?? get_option( 'blog_charset' ) );
}

Changelog

Version Description
6.6.1 A wrapper for _is_utf8_charset
6.6.0 Introduced.