URLify.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * A PHP port of URLify.js from the Django project
  4. * (https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js).
  5. * Handles symbols from Latin languages, Greek, Turkish, Russian, Ukrainian,
  6. * Czech, Polish, and Latvian. Symbols it cannot transliterate
  7. * it will simply omit.
  8. *
  9. * Usage:
  10. *
  11. * echo URLify::filter (' J\'étudie le français ');
  12. * // "jetudie-le-francais"
  13. *
  14. * echo URLify::filter ('Lo siento, no hablo español.');
  15. * // "lo-siento-no-hablo-espanol"
  16. */
  17. class URLify {
  18. public static $maps = array (
  19. 'de' => array ( /* German */
  20. 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss',
  21. 'ẞ' => 'SS'
  22. ),
  23. 'latin' => array (
  24. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A', 'Æ' => 'AE', 'Ç' =>
  25. 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
  26. 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' =>
  27. 'O', 'Ő' => 'O', 'Ø' => 'O', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
  28. 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' =>
  29. 'a', 'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
  30. 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
  31. 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'ù' => 'u', 'ú' => 'u',
  32. 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y'
  33. ),
  34. 'latin_symbols' => array (
  35. '©' => '(c)'
  36. ),
  37. 'el' => array ( /* Greek */
  38. 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
  39. 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
  40. 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
  41. 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
  42. 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
  43. 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
  44. 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
  45. 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
  46. 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
  47. 'Ϋ' => 'Y'
  48. ),
  49. 'tr' => array ( /* Turkish */
  50. 'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ü' => 'u', 'Ü' => 'U',
  51. 'ö' => 'o', 'Ö' => 'O', 'ğ' => 'g', 'Ğ' => 'G'
  52. ),
  53. 'ru' => array ( /* Russian */
  54. 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
  55. 'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
  56. 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
  57. 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
  58. 'я' => 'ya',
  59. 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
  60. 'З' => 'Z', 'И' => 'I', 'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
  61. 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
  62. 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
  63. 'Я' => 'Ya'
  64. ),
  65. 'uk' => array ( /* Ukrainian */
  66. 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g'
  67. ),
  68. 'cs' => array ( /* Czech */
  69. 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
  70. 'ž' => 'z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T',
  71. 'Ů' => 'U', 'Ž' => 'Z'
  72. ),
  73. 'pl' => array ( /* Polish */
  74. 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
  75. 'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S',
  76. 'Ź' => 'Z', 'Ż' => 'Z'
  77. ),
  78. 'lv' => array ( /* Latvian */
  79. 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
  80. 'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i',
  81. 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z'
  82. ),
  83. 'lt' => array ( /* Lithuanian */
  84. 'ą' => 'a', 'č' => 'c', 'ę' => 'e', 'ė' => 'e', 'į' => 'i', 'š' => 's', 'ų' => 'u', 'ū' => 'u', 'ž' => 'z',
  85. 'Ą' => 'A', 'Č' => 'C', 'Ę' => 'E', 'Ė' => 'E', 'Į' => 'I', 'Š' => 'S', 'Ų' => 'U', 'Ū' => 'U', 'Ž' => 'Z'
  86. )
  87. );
  88. /**
  89. * List of words to remove from URLs.
  90. */
  91. public static $remove_list = array (
  92. 'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from',
  93. 'is', 'in', 'into', 'like', 'of', 'off', 'on', 'onto', 'per',
  94. 'since', 'than', 'the', 'this', 'that', 'to', 'up', 'via',
  95. 'with'
  96. );
  97. /**
  98. * The character map.
  99. */
  100. private static $map = array ();
  101. /**
  102. * The character list as a string.
  103. */
  104. private static $chars = '';
  105. /**
  106. * The character list as a regular expression.
  107. */
  108. private static $regex = '';
  109. /**
  110. * The current language
  111. */
  112. private static $language = '';
  113. /**
  114. * Initializes the character map.
  115. */
  116. private static function init ($language = "") {
  117. if (count (self::$map) > 0 && (($language == "") || ($language == self::$language))) {
  118. return;
  119. }
  120. /* Is a specific map associated with $language ? */
  121. if (isset(self::$maps[$language]) && is_array(self::$maps[$language])) {
  122. /* Move this map to end. This means it will have priority over others */
  123. $m = self::$maps[$language];
  124. unset(self::$maps[$language]);
  125. self::$maps[$language] = $m;
  126. /* Reset static vars */
  127. self::$language = $language;
  128. self::$map = array();
  129. self::$chars = '';
  130. self::$regex = '';
  131. }
  132. foreach (self::$maps as $map) {
  133. foreach ($map as $orig => $conv) {
  134. self::$map[$orig] = $conv;
  135. self::$chars .= $orig;
  136. }
  137. }
  138. self::$regex = '/[' . self::$chars . ']/u';
  139. }
  140. /**
  141. * Add new characters to the list. `$map` should be a hash.
  142. */
  143. public static function add_chars ($map) {
  144. if (! is_array ($map)) {
  145. throw new LogicException ('$map must be an associative array.');
  146. }
  147. self::$maps[] = $map;
  148. self::$map = array ();
  149. self::$chars = '';
  150. }
  151. /**
  152. * Append words to the remove list. Accepts either single words
  153. * or an array of words.
  154. */
  155. public static function remove_words ($words) {
  156. $words = is_array ($words) ? $words : array ($words);
  157. self::$remove_list = array_merge (self::$remove_list, $words);
  158. }
  159. /**
  160. * Transliterates characters to their ASCII equivalents.
  161. * $language specifies a priority for a specific language.
  162. * The latter is useful if languages have different rules for the same character.
  163. */
  164. public static function downcode ($text, $language = "") {
  165. self::init ($language);
  166. if (preg_match_all (self::$regex, $text, $matches)) {
  167. for ($i = 0; $i < count ($matches[0]); $i++) {
  168. $char = $matches[0][$i];
  169. if (isset (self::$map[$char])) {
  170. $text = str_replace ($char, self::$map[$char], $text);
  171. }
  172. }
  173. }
  174. return $text;
  175. }
  176. /**
  177. * Filters a string, e.g., "Petty theft" to "petty-theft"
  178. */
  179. public static function filter ($text, $length = 60, $language = "") {
  180. $text = self::downcode ($text,$language);
  181. // remove all these words from the string before urlifying
  182. $text = preg_replace ('/\b(' . join ('|', self::$remove_list) . ')\b/i', '', $text);
  183. // if downcode doesn't hit, the char will be stripped here
  184. $text = preg_replace ('/[^-\w\s]/', '', $text); // remove unneeded chars
  185. $text = preg_replace ('/^\s+|\s+$/', '', $text); // trim leading/trailing spaces
  186. $text = preg_replace ('/[-\s]+/', '-', $text); // convert spaces to hyphens
  187. $text = strtolower ($text); // convert to lowercase
  188. return trim (substr ($text, 0, $length), '-'); // trim to first $length chars
  189. }
  190. /**
  191. * Alias of `URLify::downcode()`.
  192. */
  193. public static function transliterate ($text) {
  194. return self::downcode ($text);
  195. }
  196. }
  197. ?>