You are here

function _collapse_text_id_safe in Collapse Text 6.2

Converts a string to a suitable html ID attribute. Copied from zen_id_safe() in the Zen theme.

http://www.w3.org/TR/html4/struct/global.html#h-7.5.2 specifies what makes a valid ID attribute in HTML. This function:

  • Ensure an ID starts with an alpha character by optionally adding an 'id'.
  • Replaces any character except alphanumeric characters with dashes.
  • Converts entire string to lowercase.

Parameters

$string: The string

Return value

The converted string

1 call to _collapse_text_id_safe()
_collapse_text_process_child_item in ./collapse_text.module
process a child item.

File

./collapse_text.module, line 664
collapse_text is an input filter that allows text to be collapsible

Code

function _collapse_text_id_safe($string) {

  // Replace with dashes anything that isn't A-Z, numbers, dashes, or underscores.
  $string = strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));

  // If the first character is not a-z, add 'id' in front.
  if (!ctype_lower($string[0])) {

    // Don't use ctype_alpha since its locale aware.
    $string = 'id' . $string;
  }
  return $string;
}