tag_escape()WP 2.5.0

Cleans an HTML tag name. Removes all characters except a-zA-Z0-9_:. Converts the string to lowercase (lowercase letters).

1 time — 0.000001 sec (speed of light) | 50000 times — 0.05 sec (speed of light) | PHP 7.2.16, WP 5.2
Hooks from the function

Returns

String. A string cleaned for use as an HTML tag name.

Usage

tag_escape( $tag_name );
$tag_name(string) (required)
The string that should be the name of the HTML tag, e.g.: div, span, var, a, em ...

Examples

0

#1 Check the HTML tag name

echo tag_escape( '#1 I! Love this WORLD $@#%#$' ); // 1ilovethisworld

echo tag_escape( 'DIV' );   // div
echo tag_escape( '<div>' ); // div

Changelog

Since 2.5.0 Introduced.
Since 6.5.5 Allow hyphens in tag names (i.e. custom elements).

tag_escape() code WP 6.8.3

function tag_escape( $tag_name ) {
	$safe_tag = strtolower( preg_replace( '/[^a-zA-Z0-9-_:]/', '', $tag_name ) );
	/**
	 * Filters a string cleaned and escaped for output as an HTML tag.
	 *
	 * @since 2.8.0
	 *
	 * @param string $safe_tag The tag name after it has been escaped.
	 * @param string $tag_name The text before it was escaped.
	 */
	return apply_filters( 'tag_escape', $safe_tag, $tag_name );
}