WP_HTML_Processor::escape_text_for_serializationprivate staticWP 7.1.0

Escapes decoded text for HTML serialization.

Use for:

  • Attribute values.
  • Text in ordinary (data-state) elements and in the RCDATA elements (TITLE and TEXTAREA).
  • Text in foreign content (elements not in the HTML namespace).

Do not use for text in the RAWTEXT elements (STYLE, XMP, IFRAME, NOEMBED, NOFRAMES), HTML SCRIPT elements, or PLAINTEXT elements, whose contents serialize without escaping.

Method of the class: WP_HTML_Processor{}

No Hooks.

Returns

String. Escaped text.

Usage

$result = WP_HTML_Processor::escape_text_for_serialization( $text ): string;
$text(string) (required)
Decoded text to escape.

Changelog

Since 7.1.0 Introduced.

WP_HTML_Processor::escape_text_for_serialization() code WP 7.1

private static function escape_text_for_serialization( string $text ): string {
	$text = htmlspecialchars( $text, ENT_QUOTES | ENT_SUBSTITUTE | ENT_HTML5, 'UTF-8' );

	$text = str_replace( "\r", '
', $text );

	return str_replace( "\x00", "\u{FFFD}", $text );
}