Browse Source

updated transliteration (greek + russian + german + ukrainian) and removed dependency on ascii function

tags/2.0.0-beta
Antonio Ramirez 12 years ago
parent
commit
10c52d702f
  1. 135
      framework/yii/helpers/base/Inflector.php

135
framework/yii/helpers/base/Inflector.php

@ -214,59 +214,90 @@ class Inflector
'Yengeese' => 'Yengeese', 'Yengeese' => 'Yengeese',
); );
/** /**
* @var array map of special chars and its translation. This is used by [[slug()]]. * @var array map of special chars and its translation. This is used by [[slug()]] and [[ascii()]].
*/ */
public static $transliteration = array( protected static $transliteration = array(
'/Æ|Ǽ/' => 'AE',
'/ä|æ|ǽ/' => 'ae', '/ä|æ|ǽ/' => 'ae',
'/ö|œ/' => 'oe', '/ö|œ/' => 'oe',
'/ü/' => 'ue', '/ü/' => 'ue',
'/Ä/' => 'Ae', '/Ä/' => 'Ae',
'/Ü/' => 'Ue', '/Ü/' => 'Ue',
'/Ö/' => 'Oe', '/Ö/' => 'Oe',
'/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A', '/Ψ/' => 'PS',
'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a', '/ψ/' => 'ps',
'/Ç|Ć|Ĉ|Ċ|Č/' => 'C', '/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ|Ά/' => 'A',
'/ç|ć|ĉ|ċ|č/' => 'c', '/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª|ά/' => 'a',
'/Ð|Ď|Đ/' => 'D', '/Б/' => 'B',
'/ð|ď|đ/' => 'd', '/β|б/' => 'b',
'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E', '/Ç|Ć|Ĉ|Ċ|Č|Ц/' => 'C',
'/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e', '/ç|ć|ĉ|ċ|č|ц/' => 'c',
'/Ĝ|Ğ|Ġ|Ģ/' => 'G', '/Ч/' => 'Ch',
'/ĝ|ğ|ġ|ģ/' => 'g', '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ŷ|Ύ|Ϋ/' => 'Y',
'/ý|ÿ|ŷ|υ|ύ|ΰ|ы/' => 'y',
'/Ŵ|Ω|Ώ/' => 'W',
'/ŵ|ω|ώ/' => 'w',
'/Ź|Ż|Ž|З/' => 'Z',
'/ź|ż|ž|ζ|з/' => 'z',
'/IJ/' => 'IJ', '/IJ/' => 'IJ',
'/ij/' => 'ij', '/ij/' => 'ij',
'/Œ/' => 'OE', '/Œ/' => 'OE',
'/ƒ/' => 'f' '/Ш|Щ/' => 'Sh',
'/ш|щ/' => 'sh',
'/Я/' => 'Ya',
'/я/' => 'ya',
'/Є/' => 'Ye',
'/є/' => 'ye',
'/Ї/' => 'Yi',
'/ї/' => 'yi',
'/Ё/' => 'Yo',
'/ё/' => 'yo',
'/Ю/' => 'Yu',
'/ю/' => 'yu',
'/Ж/' => 'Zh',
'/ж/' => 'zh',
'/ξ|Ξ/' => '3',
'/θ/' => '8',
'/ъ|ь|Ъ|Ы|Ь/' => '',
); );
/** /**
@ -431,21 +462,19 @@ class Inflector
/** /**
* Returns a string with all spaces converted to given replacement and * Returns a string with all spaces converted to given replacement and
* non word characters removed. Maps special characters to ASCII using * non word characters removed. Maps special characters to ASCII using [[ascii()]]
* `Inflector::$transliteration`
* @param string $string An arbitrary string to convert * @param string $string An arbitrary string to convert
* @param string $replacement The replacement to use for spaces * @param string $replacement The replacement to use for spaces
* @return string The converted string. * @return string The converted string.
*/ */
public static function slug($string, $replacement = '-') public static function slug($string, $replacement = '-')
{ {
$map = static::$transliteration + array( $map = array(
'/[^\w\s]/' => ' ', '/\\s+/' => $replacement,
'/\\s+/' => $replacement, '/(?<=[a-z])([A-Z])/' => $replacement . '\\1',
'/(?<=[a-z])([A-Z])/' => $replacement . '\\1', str_replace(':rep', preg_quote($replacement, '/'), '/^[:rep]+|[:rep]+$/') => ''
str_replace(':rep', preg_quote($replacement, '/'), '/^[:rep]+|[:rep]+$/') => '' );
); return preg_replace(array_keys($map), array_values($map), static::ascii($string));
return preg_replace(array_keys($map), array_values($map), $string);
} }
/** /**
@ -479,15 +508,11 @@ class Inflector
/**+ /**+
* Converts all special characters to the closest ascii character equivalent. * Converts all special characters to the closest ascii character equivalent.
* @param string $string the string to be converted. * @param string $string the string to be converted.
* @param array $replace the characters to be replaced by spaces.
* @return string the translated * @return string the translated
*/ */
public static function ascii($string, $replace = array()) public static function ascii($string)
{ {
if (!empty($replace)) { $map = static::$transliteration + array('/[^\w\s]/' => ' ');
$string = str_replace((array)$replace, ' ', $string); return preg_replace(array_keys($map), array_values($map), $string);
}
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
return preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $string);
} }
} }

Loading…
Cancel
Save