WordPress at a glance

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 ''

Strip whitespace (or other characters) from the beginning and end of a string. See trim()

Works based on strip_tags()

Is the basis for: sanitize_text_field()
✈ 1 time = 0.000037s = very fast | 50000 times = 0.93s = very fast

No Hooks.

Return

String. The processed string.

Usage

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

Examples

#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.

Code of wp strip all tags: wp-includes/formatting.php WP 5.2.4

<?php
function wp_strip_all_tags( $string, $remove_breaks = false ) {
	$string = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $string );
	$string = strip_tags( $string );

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

	return trim( $string );
}

Related Functions

From tag: esc_ (clean validate sanitize)

More from category: Sanitizing, Escaping

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register