sanitize_key( string $key )

Sanitizes a string key.


Description

Keys are used as internal identifiers. Lowercase alphanumeric characters, dashes, and underscores are allowed.


Top ↑

Parameters

$key

(string) (Required) String key.


Top ↑

Return

(string) Sanitized key.


Top ↑

Source

File: wp-includes/formatting.php

function sanitize_key( $key ) {
	$sanitized_key = '';

	if ( is_scalar( $key ) ) {
		$sanitized_key = strtolower( $key );
		$sanitized_key = preg_replace( '/[^a-z0-9_\-]/', '', $sanitized_key );
	}

	/**
	 * Filters a sanitized key string.
	 *
	 * @since 3.0.0
	 *
	 * @param string $sanitized_key Sanitized key.
	 * @param string $key           The key prior to sanitization.
	 */
	return apply_filters( 'sanitize_key', $sanitized_key, $key );
}


Top ↑

Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    <?php
    $sankey = sanitize_key('Testexample1-_/[]{}');
    echo $sankey;
    ?>
    

    In above code use one uppercase & other lowercase letters, number(1), dash, underscore, forward slash, brackets. So sanitize_key() function change one uppercase letter into lowercase, remove forward slash & brackets.

    Output:
    testexample1-_

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