WP_HTML_Decoder::decode │ public static │ WP 6.6.0

Decodes a span of HTML text, depending on the context in which it's found.

This is a low-level method; prefer calling WP_HTML_Decoder::decode_attribute() or WP_HTML_Decoder::decode_text_node() instead. It's provided for cases where this may be difficult to do from calling code.

Example:

'©' = WP_HTML_Decoder::decode( 'data', '©' );

Метод класса: WP_HTML_Decoder{}

Внутренняя функция — эта функция рассчитана на использование самим ядром. Не рекомендуется использовать эту функцию в своем коде.

Хуков нет.

Возвращает

string. Decoded UTF-8 string.

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

$result = WP_HTML_Decoder::decode( $context, $text ): string;
$context(строка) (обязательный)
attribute for decoding attribute values, data otherwise.
$text(строка) (обязательный)
Text document containing span of text to decode.

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

С версии 6.6.0 Введена.

Код WP_HTML_Decoder::decode() WP 7.1.4

public static function decode( $context, $text ): string {
	$decoded = '';
	$end     = strlen( $text );
	$at      = 0;
	$was_at  = 0;

	while ( $at < $end ) {
		$next_character_reference_at = strpos( $text, '&', $at );
		if ( false === $next_character_reference_at ) {
			break;
		}

		$character_reference = self::read_character_reference( $context, $text, $next_character_reference_at, $token_length );
		if ( isset( $character_reference ) ) {
			$at       = $next_character_reference_at;
			$decoded .= substr( $text, $was_at, $at - $was_at );
			$decoded .= $character_reference;
			$at      += $token_length;
			$was_at   = $at;
			continue;
		}

		++$at;
	}

	if ( 0 === $was_at ) {
		return $text;
	}

	if ( $was_at < $end ) {
		$decoded .= substr( $text, $was_at, $end - $was_at );
	}

	return $decoded;
}