WP_HTML_Decoder::decode( string $context, string $text ): string

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

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

Description

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', '©' );

Parameters

$contextstringrequired
attribute for decoding attribute values, data otherwise.
$textstringrequired
Text document containing span of text to decode.

Return

string Decoded UTF-8 string.

Source

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;
}

Changelog

VersionDescription
6.6.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.