wp_strip_all_tags()WP 2.9.0

Removes all HTML tags from passed content. Script/Style tags removed with their content.

This differs from strip_tags() because it removes the contents of the <script> and <style> tags. For example:

strip_tags( '<script>something</script>' ); // something
wp_strip_all_tags( '<script>something</script>' ); // empty ''

Works based on strip_tags()

Use trim() to Strip whitespace from the beginning and end of a string.

1 time — 0.000001 sec (speed of light) | 50000 times — 0.07 sec (speed of light) | PHP 7.4.8, WP 5.6.1

No Hooks.

Return

String. The processed string.

Usage

wp_strip_all_tags( $text, $remove_breaks );
$text(string) (required)
String containing HTML tags
$remove_breaks(true|false)
Whether to remove left over line breaks and white space chars
Default: false

Examples

2

#1 Strip all HTML tags

Strips all HTML tags from the string, so it becomes super safe to display it.

$str = '<script>code</script> 11<br> 22 <strong>333</strong> ';

$str = wp_strip_all_tags( $str, 0 );

// $str contains
// '11 22 333'

Changelog

Since 2.9.0 Introduced.

wp_strip_all_tags() code WP 6.5.3

function wp_strip_all_tags( $text, $remove_breaks = false ) {
	if ( is_null( $text ) ) {
		return '';
	}

	if ( ! is_scalar( $text ) ) {
		/*
		 * To maintain consistency with pre-PHP 8 error levels,
		 * trigger_error() is used to trigger an E_USER_WARNING,
		 * rather than _doing_it_wrong(), which triggers an E_USER_NOTICE.
		 */
		trigger_error(
			sprintf(
				/* translators: 1: The function name, 2: The argument number, 3: The argument name, 4: The expected type, 5: The provided type. */
				__( 'Warning: %1$s expects parameter %2$s (%3$s) to be a %4$s, %5$s given.' ),
				__FUNCTION__,
				'#1',
				'$text',
				'string',
				gettype( $text )
			),
			E_USER_WARNING
		);

		return '';
	}

	$text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
	$text = strip_tags( $text );

	if ( $remove_breaks ) {
		$text = preg_replace( '/[\r\n\t ]+/', ' ', $text );
	}

	return trim( $text );
}