8.5.x Html.php Html::getId($id)
8.0.x Html.php Html::getId($id)
8.1.x Html.php Html::getId($id)
8.2.x Html.php Html::getId($id)
8.3.x Html.php Html::getId($id)
8.4.x Html.php Html::getId($id)
8.6.x Html.php Html::getId($id)

Prepares a string for use as a valid HTML ID.

Only use this function when you want to intentionally skip the uniqueness guarantee of self::getUniqueId().


string $id: The ID to clean.

Return value

string The cleaned ID.

See also


6 calls to Html::getId()
ExposedFormTest::testExposedIdentifier in core/modules/views/src/Tests/Plugin/ExposedFormTest.php
Tests the exposed form with a non-standard identifier.
FormBuilder::doBuildForm in core/lib/Drupal/Core/Form/FormBuilder.php
Builds and processes all elements in the structured form array.
FormBuilder::prepareForm in core/lib/Drupal/Core/Form/FormBuilder.php
Prepares a structured form array.
Html::getUniqueId in core/lib/Drupal/Component/Utility/Html.php
Prepares a string for use as a valid HTML ID and guarantees uniqueness.
HtmlTest::testHtmlGetId in core/tests/Drupal/Tests/Component/Utility/HtmlTest.php
Tests the Html::getUniqueId() method.

... See full list


core/lib/Drupal/Component/Utility/Html.php, line 215


Provides DOMDocument helpers for parsing and serializing HTML strings.




public static function getId($id) {
  $id = str_replace([
    ' ',
  ], [
  ], Unicode::strtolower($id));

  // 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);

  // Removing multiple consecutive hyphens.
  $id = preg_replace('/\\-+/', '-', $id);
  return $id;