sanitize_html_class

函数
sanitize_html_class ( $class, $fallback = '' )
参数
  • (string) $class The classname to be sanitized
    Required:
  • (string) $fallback Optional. The value to return if the sanitization ends up as an empty string. Defaults to an empty string.
    Required:
    Default: (empty)
返回值
  • (string) The sanitized value
定义位置
相关方法
sanitize_hex_colorsanitize_metasanitize_termsanitize_term_fieldsanitize_email
引入
2.8.0
弃用
-

sanitize_html_class: 这是一个WordPress的函数,它对一个HTML类的名称进行净化。它用于删除任何潜在的危险元素,如脚本标签或无效字符: 这个函数有一个参数,就是要净化的类名。

对一个HTML类名进行净化,以确保它只包含有效的字符。

将字符串剥离为A-Z、a-z、0-9、_、-。如果这样做的结果是一个空字符串,那么它将返回所提供的替代值。

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 );
}

常见问题

FAQs
查看更多 >