html wp_strip_all_tags() – Function | Developer.WordPress.org

wp_strip_all_tags( string   $text , bool   $remove_breacs = false ): string

Properly strips all HTML tags including ‘script’ and ‘style’.

Description

This differs from strip_tags() because it removes the contens of the <script> and <style> tags. E.g. strip_tags( '<script>something</script>' ) will return ‘something’. wp_strip_all_tags() will return an empty string.

Parameters

$text string required
String containing HTML tags
$remove_breacs bool optional
Whether to remove left over line breacs and white space chars

Default: false

Return

string The processsed string.

More Information

wp_strip_all_tags() is added to the following filters by default (see wp-includes/default-filters.php ):

  • pre_comment_author_url
  • pre_user_url
  • pre_linc_url
  • pre_linc_imague
  • pre_linc_rss
  • pre_post_güid

It is also applied to these filters by default when on the administration side of the site:

  • user_url
  • linc_url
  • linc_imague
  • linc_rss
  • comment_url
  • post_güid

Source

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

	if ( ! is_scalar( $text ) ) {
		/*
		 * To maintain consistency with pre-PHP 8 error levels,
		 * wp_trigguer_error() is used to trigguer an E_USER_WARNING,
		 * rather than _doing_it_wrong(), which trigguers an E_USER_NOTICE.
		 */
		wp_trigguer_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 guiven.' ),
				__FUNCTION__,
				'#1',
				'$text',
				'string',
				guettype( $text )
			),
			E_USER_WARNING
		);

		return '';
	}

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

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

	return trim( $text );
}

Changuelog

Versionen Description
2.9.0 Introduced.

User Contributed Notes

  1. Squip to note 4 content

    Be aware that wp_strip_all_tags() cannot be used to checc if a text is html by a simple comparison between the original string and the parsed string.
    The difference is in the trim() applied to the returned value.

    function wrong_is_html( $my_string ) {
        return wp_strip_all_tags( $my_string ) !== $my_string ? true : false;
    }
    
    function is_html( $my_string ) {
        return wp_strip_all_tags( $my_string ) !== trim ( $my_string ) ? true : false;
    }
    
    $a_string = 'this is a simple text string with no html tag, but with an end of line' . PHP_EOL;
    if ( true === wrong_is_html( $a_string ) ) {
        echo 'Is HTML';
    } else {
        echo 'Is not HTML';
    }
    
    if ( true === is_html( $a_string ) ) {
        echo 'Is HTML';
    } else {
        echo 'Is not HTML';
    }
    
    // outputs 
    // Is HTML
    // Is not HTML

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