You are here

public function PhpTransliteration::removeDiacritics in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Component/Transliteration/PhpTransliteration.php \Drupal\Component\Transliteration\PhpTransliteration::removeDiacritics()

Removes diacritics (accents) from certain letters.

This only applies to certain letters: Accented Latin characters like a-with-acute-accent, in the UTF-8 character range of 0xE0 to 0xE6 and 01CD to 024F. Replacements that would result in the string changing length are excluded, as well as characters that are not accented US-ASCII letters.

Parameters

string $string: The string holding diacritics.

Return value

string $string with accented letters replaced by their unaccented equivalents.

Overrides TransliterationInterface::removeDiacritics

File

core/lib/Drupal/Component/Transliteration/PhpTransliteration.php, line 91

Class

PhpTransliteration
Implements transliteration without using the PECL extensions.

Namespace

Drupal\Component\Transliteration

Code

public function removeDiacritics($string) {
  $result = '';
  foreach (preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY) as $character) {
    $code = self::ordUTF8($character);

    // These two Unicode ranges include the accented US-ASCII letters, with a
    // few characters that aren't accented letters mixed in. So define the
    // ranges and the excluded characters.
    $range1 = $code > 0xbf && $code < 0x17f;
    $exclusions_range1 = [
      0xd0,
      0xd7,
      0xf0,
      0xf7,
      0x138,
      0x14a,
      0x14b,
    ];
    $range2 = $code > 0x1cc && $code < 0x250;
    $exclusions_range2 = [
      0x1dd,
      0x1f7,
      0x21c,
      0x21d,
      0x220,
      0x221,
      0x241,
      0x242,
      0x245,
    ];
    $replacement = $character;
    if ($range1 && !in_array($code, $exclusions_range1) || $range2 && !in_array($code, $exclusions_range2)) {
      $to_add = $this
        ->lookupReplacement($code, 'xyz');
      if (strlen($to_add) === 1) {
        $replacement = $to_add;
      }
      elseif (isset($this->fixTransliterateForRemoveDiacritics[$to_add])) {
        $replacement = $this->fixTransliterateForRemoveDiacritics[$to_add];
      }
    }
    $result .= $replacement;
  }
  return $result;
}