You are here

function collapse_text_id_safe in Collapse Text 6

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_replace_callback in ./collapse_text.module

File

./collapse_text.module, line 251
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;
}