function drupal_clean_css_identifier in Drupal 7
Prepares a string for use as a CSS identifier (element, class, or ID name).
http://www.w3.org/TR/CSS21/syndata.html#characters shows the syntax for valid CSS identifiers (including element names, classes, and IDs in selectors.)
Parameters
$identifier: The identifier to clean.
$filter: An array of string replacements to use on the identifier.
Return value
The cleaned identifier.
2 calls to drupal_clean_css_identifier()
- DrupalHTMLIdentifierTestCase::testDrupalCleanCSSIdentifier in modules/
simpletest/ tests/ common.test - Tests that drupal_clean_css_identifier() cleans the identifier properly.
- drupal_html_class in includes/
common.inc - Prepares a string for use as a valid class name.
File
- includes/
common.inc, line 3951 - Common functions that many Drupal modules will need to reference.
Code
function drupal_clean_css_identifier($identifier, $filter = array(
' ' => '-',
'_' => '-',
'/' => '-',
'[' => '-',
']' => '',
)) {
// Use the advanced drupal_static() pattern, since this is called very often.
static $drupal_static_fast;
if (!isset($drupal_static_fast)) {
$drupal_static_fast['allow_css_double_underscores'] =& drupal_static(__FUNCTION__ . ':allow_css_double_underscores');
}
$allow_css_double_underscores =& $drupal_static_fast['allow_css_double_underscores'];
if (!isset($allow_css_double_underscores)) {
$allow_css_double_underscores = variable_get('allow_css_double_underscores', FALSE);
}
// Preserve BEM-style double-underscores depending on custom setting.
if ($allow_css_double_underscores) {
$filter['__'] = '__';
}
// By default, we filter using Drupal's coding standards.
$identifier = strtr($identifier, $filter);
// Valid characters in a CSS identifier are:
// - the hyphen (U+002D)
// - a-z (U+0030 - U+0039)
// - A-Z (U+0041 - U+005A)
// - the underscore (U+005F)
// - 0-9 (U+0061 - U+007A)
// - ISO 10646 characters U+00A1 and higher
// We strip out any character not in the above list.
$identifier = preg_replace('/[^\\x{002D}\\x{0030}-\\x{0039}\\x{0041}-\\x{005A}\\x{005F}\\x{0061}-\\x{007A}\\x{00A1}-\\x{FFFF}]/u', '', $identifier);
return $identifier;
}