From 35236d8291b7b183ed9b04454fd53c3f929945a9 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 16 Mar 2010 19:19:32 +0000 Subject: [PATCH] Introduce sanitize_key(). git-svn-id: https://develop.svn.wordpress.org/trunk@13718 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/formatting.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index 7e73117571..39416954e5 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -750,6 +750,31 @@ function sanitize_user( $username, $strict = false ) { return apply_filters('sanitize_user', $username, $raw_username, $strict); } +/** + * Sanitize a string key. + * + * Keys are used as internal identifiers. They should be lowercase ASCII. Dashes and underscores are allowed. + * + * @since 3.0.0 + * + * @param string $key String key + * @return string Sanitized key + */ +function sanitize_key( $key ) { + $raw_key = $key; + $key = wp_strip_all_tags($key); + // Kill octets + $key = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $key); + $key = preg_replace('/&.+?;/', '', $key); // Kill entities + + $key = preg_replace('|[^a-z0-9 _.\-@]|i', '', $key); + + // Consolidate contiguous whitespace + $key = preg_replace('|\s+|', ' ', $key); + + return apply_filters('sanitize_key', $key, $raw_key); +} + /** * Sanitizes title or use fallback title. *