public function PhpTransliteration::transliterate in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Component/Transliteration/PhpTransliteration.php \Drupal\Component\Transliteration\PhpTransliteration::transliterate()
- 9 core/lib/Drupal/Component/Transliteration/PhpTransliteration.php \Drupal\Component\Transliteration\PhpTransliteration::transliterate()
File
- core/lib/Drupal/Component/Transliteration/PhpTransliteration.php, line 125
Class
- PhpTransliteration
- Implements transliteration without using the PECL extensions.
Namespace
Drupal\Component\Transliteration
Code
public function transliterate($string, $langcode = 'en', $unknown_character = '?', $max_length = NULL) {
$result = '';
$length = 0;
$hash = FALSE;
if ($unknown_character != '?' && strpos($string, '?') !== FALSE) {
$hash = hash('sha256', $string);
$string = str_replace('?', $hash, $string);
}
$string = mb_convert_encoding($string, 'UTF-8', 'UTF-8');
if ($unknown_character != '?') {
$string = str_replace('?', $unknown_character, $string);
if ($hash !== FALSE) {
$string = str_replace($hash, '?', $string);
}
}
foreach (preg_split('//u', $string, 0, PREG_SPLIT_NO_EMPTY) as $character) {
$code = self::ordUTF8($character);
if ($code == -1) {
$to_add = $unknown_character;
}
else {
$to_add = $this
->replace($code, $langcode, $unknown_character);
}
if (isset($max_length)) {
$length += strlen($to_add);
if ($length > $max_length) {
return $result;
}
}
$result .= $to_add;
}
return $result;
}