函数文档

convert_invalid_entities()

💡 云策文档标注

概述

convert_invalid_entities() 函数用于将无效的 Unicode 引用范围转换为有效范围,主要处理 Windows CP1252 特定字符,以确保在非 Windows 浏览器上正确显示。

关键要点

  • 函数接受一个字符串参数 $content,包含需要转换的实体。
  • 返回转换后的字符串。
  • 内部使用 $wp_htmltranswinuni 数组映射无效字符到有效字符,例如将 '€' 转换为 '€'。
  • 仅当字符串包含 '' 时才执行转换,使用 strtr() 函数进行替换。

代码示例

function convert_invalid_entities( $content ) {
    $wp_htmltranswinuni = array(
        '€' => '€', // The Euro sign.
        '' => '',
        '‚' => '‚', // These are Windows CP1252 specific characters.
        'ƒ' => 'ƒ',  // They would look weird on non-Windows browsers.
        '„' => '„',
        '…' => '…',
        '†' => '†',
        '‡' => '‡',
        'ˆ' => 'ˆ',
        '‰' => '‰',
        'Š' => 'Š',
        '‹' => '‹',
        'Œ' => 'Œ',
        '' => '',
        'Ž' => 'Ž',
        '' => '',
        '' => '',
        '‘' => '‘',
        '’' => '’',
        '“' => '“',
        '”' => '”',
        '•' => '•',
        '–' => '–',
        '—' => '—',
        '˜' => '˜',
        '™' => '™',
        'š' => 'š',
        '›' => '›',
        'œ' => 'œ',
        '' => '',
        'ž' => 'ž',
        'Ÿ' => 'Ÿ',
    );

    if ( str_contains( $content, '' ) ) {
        $content = strtr( $content, $wp_htmltranswinuni );
    }

    return $content;
}

注意事项

  • 此函数自 WordPress 4.3.0 版本引入。
  • 主要用于处理从 Windows 系统导入或生成的文本,以避免字符显示问题。
  • 转换基于预定义的映射数组,仅针对特定无效字符进行替换。

📄 原文内容

Converts invalid Unicode references range to valid range.

Parameters

$contentstringrequired
String with entities that need converting.

Return

string Converted string.

Source

function convert_invalid_entities( $content ) {
	$wp_htmltranswinuni = array(
		'€' => '€', // The Euro sign.
		'' => '',
		'‚' => '‚', // These are Windows CP1252 specific characters.
		'ƒ' => 'ƒ',  // They would look weird on non-Windows browsers.
		'„' => '„',
		'…' => '…',
		'†' => '†',
		'‡' => '‡',
		'ˆ' => 'ˆ',
		'‰' => '‰',
		'Š' => 'Š',
		'‹' => '‹',
		'Œ' => 'Œ',
		'' => '',
		'Ž' => 'Ž',
		'' => '',
		'' => '',
		'‘' => '‘',
		'’' => '’',
		'“' => '“',
		'”' => '”',
		'•' => '•',
		'–' => '–',
		'—' => '—',
		'˜' => '˜',
		'™' => '™',
		'š' => 'š',
		'›' => '›',
		'œ' => 'œ',
		'' => '',
		'ž' => 'ž',
		'Ÿ' => 'Ÿ',
	);

	if ( str_contains( $content, '' ) ) {
		$content = strtr( $content, $wp_htmltranswinuni );
	}

	return $content;
}

Changelog

Version Description
4.3.0 Introduced.