清理HTML类名以确保它只包含有效字符。

原型

sanitize_html_class( string $class, string $fallback = '' )

描述

将字符串向下剥离到A-Z,a-z,0-9,_, – 。如果这导致空字符串,那么它将返回提供的替代值。

参数

$class (string) (Required) 要清理的类名

$fallback (string) (Optional)  如果清理结束为空字符串,则返回的值。默认为空字符串。

返回值

(string)  消毒的价值

源文件

路径:wp-includes/formatting.php

<?php
...
function sanitize_html_class( $class, $fallback = '' ) {
	//Strip out any % encoded octets
	$sanitized = preg_replace( '|%[a-fA-F0-9][a-fA-F0-9]|', '', $class );
	//Limit to A-Z,a-z,0-9,_,-
	$sanitized = preg_replace( '/[^A-Za-z0-9_-]/', '', $sanitized );
	if ( '' == $sanitized && $fallback ) {
		return sanitize_html_class( $fallback );
	}
	/**
	 * Filters a sanitized HTML class string.
	 *
	 * @since 2.8.0
	 *
	 * @param string $sanitized The sanitized HTML class.
	 * @param string $class     HTML class before sanitization.
	 * @param string $fallback  The fallback string.
	 */
	return apply_filters( 'sanitize_html_class', $sanitized, $class, $fallback );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/sanitize_html_class/