drupal_html_id($id)Prepare a string for use as a valid HTML ID and guarantee uniqueness.
$id The ID to clean.
The cleaned ID.
includes/common.inc, line 3322
<?php
function drupal_html_id($id) {
$seen_ids = &drupal_static(__FUNCTION__, array());
$id = strtr(drupal_strtolower($id), array(' ' => '-', '_' => '-', '[' => '-', ']' => ''));
// As defined in http://www.w3.org/TR/html4/types.html#type-name, HTML IDs can
// only contain letters, digits ([0-9]), hyphens ("-"), underscores ("_"),
// colons (":"), and periods ("."). We strip out any character not in that
// list. Note that the CSS spec doesn't allow colons or periods in identifiers
// (http://www.w3.org/TR/CSS21/syndata.html#characters), so we strip those two
// characters as well.
$id = preg_replace('/[^A-Za-z0-9\-_]/', '', $id);
// Ensure IDs are unique. The first occurrence is held but left alone.
// Subsequent occurrences get a number appended to them. This incrementing
// will almost certainly break code that relies on explicit HTML IDs in forms
// that appear more than once on the page, but the alternative is outputting
// duplicate IDs, which would break JS code and XHTML validity anyways. For
// now, it's an acceptable stopgap solution.
if (isset($seen_ids[$id])) {
$id = $id . '-' . ++$seen_ids[$id];
}
else {
$seen_ids[$id] = 1;
}
return $id;
}
?>