Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. AbstractUnicodeString.php

function AbstractUnicodeString::ascii

Generic UTF-8 to ASCII transliteration.

Install the intl extension for best results.

Parameters

string[]|\Transliterator[]|\Closure[] $rules See "*-Latin" rules from Transliterator::listIDs():

File

vendor/symfony/string/AbstractUnicodeString.php, line 77

Class

AbstractUnicodeString
Represents a string of abstract Unicode characters.

Namespace

Symfony\Component\String

Code

public function ascii(array $rules = []) : self {
    $str = clone $this;
    $s = $str->string;
    $str->string = '';
    array_unshift($rules, 'nfd');
    $rules[] = 'latin-ascii';
    if (\function_exists('transliterator_transliterate')) {
        $rules[] = 'any-latin/bgn';
    }
    $rules[] = 'nfkd';
    $rules[] = '[:nonspacing mark:] remove';
    while (\strlen($s) - 1 > ($i = strspn($s, self::ASCII))) {
        if (0 < --$i) {
            $str->string .= substr($s, 0, $i);
            $s = substr($s, $i);
        }
        if (!($rule = array_shift($rules))) {
            $rules = [];
            // An empty rule interrupts the next ones
        }
        if ($rule instanceof \Transliterator) {
            $s = $rule->transliterate($s);
        }
        elseif ($rule instanceof \Closure) {
            $s = $rule($s);
        }
        elseif ($rule) {
            if ('nfd' === ($rule = strtolower($rule))) {
                normalizer_is_normalized($s, self::NFD) ?: ($s = normalizer_normalize($s, self::NFD));
            }
            elseif ('nfkd' === $rule) {
                normalizer_is_normalized($s, self::NFKD) ?: ($s = normalizer_normalize($s, self::NFKD));
            }
            elseif ('[:nonspacing mark:] remove' === $rule) {
                $s = preg_replace('/\\p{Mn}++/u', '', $s);
            }
            elseif ('latin-ascii' === $rule) {
                $s = str_replace(self::TRANSLIT_FROM, self::TRANSLIT_TO, $s);
            }
            elseif ('de-ascii' === $rule) {
                $s = preg_replace("/([AUO])̈(?=\\p{Ll})/u", '$1e', $s);
                $s = str_replace([
                    "ä",
                    "ö",
                    "ü",
                    "Ä",
                    "Ö",
                    "Ü",
                ], [
                    'ae',
                    'oe',
                    'ue',
                    'AE',
                    'OE',
                    'UE',
                ], $s);
            }
            elseif (\function_exists('transliterator_transliterate')) {
                if (null === ($transliterator = self::$transliterators[$rule] ??= \Transliterator::create($rule))) {
                    if ('any-latin/bgn' === $rule) {
                        $rule = 'any-latin';
                        $transliterator = self::$transliterators[$rule] ??= \Transliterator::create($rule);
                    }
                    if (null === $transliterator) {
                        throw new InvalidArgumentException(\sprintf('Unknown transliteration rule "%s".', $rule));
                    }
                    self::$transliterators['any-latin/bgn'] = $transliterator;
                }
                $s = $transliterator->transliterate($s);
            }
        }
        elseif (!\function_exists('iconv')) {
            $s = preg_replace('/[^\\x00-\\x7F]/u', '?', $s);
        }
        else {
            $s = @preg_replace_callback('/[^\\x00-\\x7F]/u', static function ($c) {
                $c = (string) iconv('UTF-8', 'ASCII//TRANSLIT', $c[0]);
                if ('' === $c && '' === iconv('UTF-8', 'ASCII//TRANSLIT', '²')) {
                    throw new \LogicException(\sprintf('"%s" requires a translit-able iconv implementation, try installing "gnu-libiconv" if you\'re using Alpine Linux.', static::class));
                }
                return 1 < \strlen($c) ? ltrim($c, '\'`"^~') : ('' !== $c ? $c : '?');
            }, $s);
        }
    }
    $str->string .= $s;
    return $str;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal