drupal_html_id

Versions
mediamosa-21
drupal_html_id($id)

Prepare a string for use as a valid HTML ID and guarantee uniqueness.

Parameters

$id The ID to clean.

Return value

The cleaned ID.

▾ 5 functions call drupal_html_id()

drupal_prepare_form in includes/form.inc
Prepares a structured form array by adding required elements, executing any hook_form_alter functions, and optionally inserting a validation token to prevent tampering.
form_builder in includes/form.inc
Walk through the structured form array, adding any required properties to each element and mapping the incoming input data to the proper elements. Also, execute any #process handlers attached to a specific element.
form_process_container in includes/form.inc
Processes a container element.
form_process_radios in includes/form.inc
Roll out a single radios element to a list of radios, using the options array as index.
form_process_tableselect in includes/form.inc
Create the correct amount of checkbox or radio elements to populate the table.

Code

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