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
$context
stringrequiredattribute
for decoding attribute values,data
otherwise.$text
stringrequired- Text document containing span of text to decode.
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
Version | Description |
---|---|
6.6.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.