WP_HTML_Decoder::decode
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', '©' );
Method of the class: WP_HTML_Decoder{}
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
String. Decoded UTF-8 string.
Usage
$result = WP_HTML_Decoder::decode( $context, $text ): string;
- $context(string) (required)
attributefor decoding attribute values,dataotherwise.- $text(string) (required)
- Text document containing span of text to decode.
Changelog
| Since 6.6.0 | Introduced. |
WP_HTML_Decoder::decode() WP HTML Decoder::decode code WP 6.9.1
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;
}