URLify.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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, Bulgarian, Russian,
  6. * Ukrainian, Czech, Polish, Romanian, Latvian, Lithuanian, Vietnamese, Arabic,
  7. * Serbian, Azerbaijani and Kazakh. Symbols it cannot transliterate
  8. * it will simply omit.
  9. *
  10. * Usage:
  11. *
  12. * echo URLify::filter (' J\'étudie le français ');
  13. * // "jetudie-le-francais"
  14. *
  15. * echo URLify::filter ('Lo siento, no hablo español.');
  16. * // "lo-siento-no-hablo-espanol"
  17. */
  18. class URLify
  19. {
  20. public static $maps = array (
  21. 'de' => array ( /* German */
  22. 'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss',
  23. 'ẞ' => 'SS'
  24. ),
  25. 'latin' => array (
  26. 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A','Ă' => 'A', 'Æ' => 'AE', 'Ç' =>
  27. 'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
  28. 'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' =>
  29. 'O', 'Ő' => 'O', 'Ø' => 'O', 'Œ' => 'OE' ,'Ș' => 'S','Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
  30. 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' =>
  31. 'a', 'å' => 'a', 'ă' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
  32. 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
  33. 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'œ' => 'oe', 'ș' => 's', 'ț' => 't', 'ù' => 'u', 'ú' => 'u',
  34. 'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y'
  35. ),
  36. 'latin_symbols' => array (
  37. '©' => '(c)'
  38. ),
  39. 'el' => array ( /* Greek */
  40. 'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
  41. 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
  42. 'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
  43. 'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
  44. 'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
  45. 'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
  46. 'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
  47. 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
  48. 'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
  49. 'Ϋ' => 'Y'
  50. ),
  51. 'tr' => array ( /* Turkish */
  52. 'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ü' => 'u', 'Ü' => 'U',
  53. 'ö' => 'o', 'Ö' => 'O', 'ğ' => 'g', 'Ğ' => 'G'
  54. ),
  55. 'bg' => array( /* Bulgarian */
  56. 'Щ' => 'Sht', 'Ш' => 'Sh', 'Ч' => 'Ch', 'Ц' => 'C', 'Ю' => 'Yu', 'Я' => 'Ya',
  57. 'Ж' => 'J', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
  58. 'Е' => 'E', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L',
  59. 'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S',
  60. 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ь' => '', 'Ъ' => 'A',
  61. 'щ' => 'sht', 'ш' => 'sh', 'ч' => 'ch', 'ц' => 'c', 'ю' => 'yu', 'я' => 'ya',
  62. 'ж' => 'j', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
  63. 'е' => 'e', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
  64. 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
  65. 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ь' => '', 'ъ' => 'a'
  66. ),
  67. 'ru' => array ( /* Russian */
  68. 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
  69. 'з' => 'z', 'и' => 'i', 'й' => 'i', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
  70. 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
  71. 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
  72. 'я' => 'ya',
  73. 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
  74. 'З' => 'Z', 'И' => 'I', 'Й' => 'I', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
  75. 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
  76. 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
  77. 'Я' => 'Ya',
  78. '№' => ''
  79. ),
  80. 'uk' => array ( /* Ukrainian */
  81. 'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g'
  82. ),
  83. 'kk' => array ( /* Kazakh */
  84. 'Ә' => 'A', 'Ғ' => 'G', 'Қ' => 'Q', 'Ң' => 'N', 'Ө' => 'O', 'Ұ' => 'U', 'Ү' => 'U', 'Һ' => 'H',
  85. 'ә' => 'a', 'ғ' => 'g', 'қ' => 'q', 'ң' => 'n', 'ө' => 'o', 'ұ' => 'u', 'ү' => 'u', 'һ' => 'h',
  86. ),
  87. 'cs' => array ( /* Czech */
  88. 'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
  89. 'ž' => 'z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T',
  90. 'Ů' => 'U', 'Ž' => 'Z'
  91. ),
  92. 'pl' => array ( /* Polish */
  93. 'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
  94. 'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S',
  95. 'Ź' => 'Z', 'Ż' => 'Z'
  96. ),
  97. 'ro' => array ( /* Romanian */
  98. 'ă' => 'a', 'â' => 'a', 'î' => 'i', 'ș' => 's', 'ț' => 't', 'Ţ' => 'T', 'ţ' => 't'
  99. ),
  100. 'lv' => array ( /* Latvian */
  101. 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
  102. 'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i',
  103. 'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z'
  104. ),
  105. 'lt' => array ( /* Lithuanian */
  106. 'ą' => 'a', 'č' => 'c', 'ę' => 'e', 'ė' => 'e', 'į' => 'i', 'š' => 's', 'ų' => 'u', 'ū' => 'u', 'ž' => 'z',
  107. 'Ą' => 'A', 'Č' => 'C', 'Ę' => 'E', 'Ė' => 'E', 'Į' => 'I', 'Š' => 'S', 'Ų' => 'U', 'Ū' => 'U', 'Ž' => 'Z'
  108. ),
  109. 'vn' => array ( /* Vietnamese */
  110. 'Á' => 'A', 'À' => 'A', 'Ả' => 'A', 'Ã' => 'A', 'Ạ' => 'A', 'Ă' => 'A', 'Ắ' => 'A', 'Ằ' => 'A', 'Ẳ' => 'A', 'Ẵ' => 'A', 'Ặ' => 'A', 'Â' => 'A', 'Ấ' => 'A', 'Ầ' => 'A', 'Ẩ' => 'A', 'Ẫ' => 'A', 'Ậ' => 'A',
  111. 'á' => 'a', 'à' => 'a', 'ả' => 'a', 'ã' => 'a', 'ạ' => 'a', 'ă' => 'a', 'ắ' => 'a', 'ằ' => 'a', 'ẳ' => 'a', 'ẵ' => 'a', 'ặ' => 'a', 'â' => 'a', 'ấ' => 'a', 'ầ' => 'a', 'ẩ' => 'a', 'ẫ' => 'a', 'ậ' => 'a',
  112. 'É' => 'E', 'È' => 'E', 'Ẻ' => 'E', 'Ẽ' => 'E', 'Ẹ' => 'E', 'Ê' => 'E', 'Ế' => 'E', 'Ề' => 'E', 'Ể' => 'E', 'Ễ' => 'E', 'Ệ' => 'E',
  113. 'é' => 'e', 'è' => 'e', 'ẻ' => 'e', 'ẽ' => 'e', 'ẹ' => 'e', 'ê' => 'e', 'ế' => 'e', 'ề' => 'e', 'ể' => 'e', 'ễ' => 'e', 'ệ' => 'e',
  114. 'Í' => 'I', 'Ì' => 'I', 'Ỉ' => 'I', 'Ĩ' => 'I', 'Ị' => 'I', 'í' => 'i', 'ì' => 'i', 'ỉ' => 'i', 'ĩ' => 'i', 'ị' => 'i',
  115. 'Ó' => 'O', 'Ò' => 'O', 'Ỏ' => 'O', 'Õ' => 'O', 'Ọ' => 'O', 'Ô' => 'O', 'Ố' => 'O', 'Ồ' => 'O', 'Ổ' => 'O', 'Ỗ' => 'O', 'Ộ' => 'O', 'Ơ' => 'O', 'Ớ' => 'O', 'Ờ' => 'O', 'Ở' => 'O', 'Ỡ' => 'O', 'Ợ' => 'O',
  116. 'ó' => 'o', 'ò' => 'o', 'ỏ' => 'o', 'õ' => 'o', 'ọ' => 'o', 'ô' => 'o', 'ố' => 'o', 'ồ' => 'o', 'ổ' => 'o', 'ỗ' => 'o', 'ộ' => 'o', 'ơ' => 'o', 'ớ' => 'o', 'ờ' => 'o', 'ở' => 'o', 'ỡ' => 'o', 'ợ' => 'o',
  117. 'Ú' => 'U', 'Ù' => 'U', 'Ủ' => 'U', 'Ũ' => 'U', 'Ụ' => 'U', 'Ư' => 'U', 'Ứ' => 'U', 'Ừ' => 'U', 'Ử' => 'U', 'Ữ' => 'U', 'Ự' => 'U',
  118. 'ú' => 'u', 'ù' => 'u', 'ủ' => 'u', 'ũ' => 'u', 'ụ' => 'u', 'ư' => 'u', 'ứ' => 'u', 'ừ' => 'u', 'ử' => 'u', 'ữ' => 'u', 'ự' => 'u',
  119. 'Ý' => 'Y', 'Ỳ' => 'Y', 'Ỷ' => 'Y', 'Ỹ' => 'Y', 'Ỵ' => 'Y', 'ý' => 'y', 'ỳ' => 'y', 'ỷ' => 'y', 'ỹ' => 'y', 'ỵ' => 'y',
  120. 'Đ' => 'D', 'đ' => 'd'
  121. ),
  122. 'ar' => array ( /* Arabic */
  123. 'أ' => 'a', 'ب' => 'b', 'ت' => 't', 'ث' => 'th', 'ج' => 'g', 'ح' => 'h', 'خ' => 'kh', 'د' => 'd',
  124. 'ذ' => 'th', 'ر' => 'r', 'ز' => 'z', 'س' => 's', 'ش' => 'sh', 'ص' => 's', 'ض' => 'd', 'ط' => 't',
  125. 'ظ' => 'th', 'ع' => 'aa', 'غ' => 'gh', 'ف' => 'f', 'ق' => 'k', 'ك' => 'k', 'ل' => 'l', 'م' => 'm',
  126. 'ن' => 'n', 'ه' => 'h', 'و' => 'o', 'ي' => 'y',
  127. 'ا' => 'a', 'إ' => 'a', 'آ' => 'a', 'ؤ' => 'o', 'ئ' => 'y', 'ء' => 'aa',
  128. '٠' => '0', '١' => '1', '٢' => '2', '٣' => '3', '٤' => '4', '٥' => '5', '٦' => '6', '٧' => '7', '٨' => '8', '٩' => '9',
  129. ),
  130. 'fa' => array ( /* Persian */
  131. 'گ' => 'g', 'ژ' => 'j', 'پ' => 'p', 'چ' => 'ch', 'ی' => 'y', 'ک' => 'k',
  132. '۰' => '0', '۱' => '1', '۲' => '2', '۳' => '3', '۴' => '4', '۵' => '5', '۶' => '6', '۷' => '7', '۸' => '8', '۹' => '9',
  133. ),
  134. 'sr' => array ( /* Serbian */
  135. 'ђ' => 'dj', 'ј' => 'j', 'љ' => 'lj', 'њ' => 'nj', 'ћ' => 'c', 'џ' => 'dz', 'đ' => 'dj',
  136. 'Ђ' => 'Dj', 'Ј' => 'j', 'Љ' => 'Lj', 'Њ' => 'Nj', 'Ћ' => 'C', 'Џ' => 'Dz', 'Đ' => 'Dj'
  137. ),
  138. 'az' => array ( /* Azerbaijani */
  139. 'ç' => 'c', 'ə' => 'e', 'ğ' => 'g', 'ı' => 'i', 'ö' => 'o', 'ş' => 's', 'ü' => 'u',
  140. 'Ç' => 'C', 'Ə' => 'E', 'Ğ' => 'G', 'İ' => 'I', 'Ö' => 'O', 'Ş' => 'S', 'Ü' => 'U'
  141. )
  142. );
  143. /**
  144. * List of words to remove from URLs.
  145. */
  146. public static $remove_list = array (
  147. 'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from',
  148. 'is', 'in', 'into', 'like', 'of', 'off', 'on', 'onto', 'per',
  149. 'since', 'than', 'the', 'this', 'that', 'to', 'up', 'via',
  150. 'with'
  151. );
  152. /**
  153. * The character map.
  154. */
  155. private static $map = array ();
  156. /**
  157. * The character list as a string.
  158. */
  159. private static $chars = '';
  160. /**
  161. * The character list as a regular expression.
  162. */
  163. private static $regex = '';
  164. /**
  165. * The current language
  166. */
  167. private static $language = '';
  168. /**
  169. * Initializes the character map.
  170. * @param string $language
  171. */
  172. private static function init ($language = "")
  173. {
  174. if (count (self::$map) > 0 && (($language == "") || ($language == self::$language))) {
  175. return;
  176. }
  177. /* Is a specific map associated with $language ? */
  178. if (isset(self::$maps[$language]) && is_array(self::$maps[$language])) {
  179. /* Move this map to end. This means it will have priority over others */
  180. $m = self::$maps[$language];
  181. unset(self::$maps[$language]);
  182. self::$maps[$language] = $m;
  183. }
  184. /* Reset static vars */
  185. self::$language = $language;
  186. self::$map = array();
  187. self::$chars = '';
  188. foreach (self::$maps as $map) {
  189. foreach ($map as $orig => $conv) {
  190. self::$map[$orig] = $conv;
  191. self::$chars .= $orig;
  192. }
  193. }
  194. self::$regex = '/[' . self::$chars . ']/u';
  195. }
  196. /**
  197. * Add new characters to the list. `$map` should be a hash.
  198. * @param array $map
  199. */
  200. public static function add_chars ($map)
  201. {
  202. if (! is_array ($map)) {
  203. throw new LogicException ('$map must be an associative array.');
  204. }
  205. self::$maps[] = $map;
  206. self::$map = array ();
  207. self::$chars = '';
  208. }
  209. /**
  210. * Append words to the remove list. Accepts either single words
  211. * or an array of words.
  212. * @param mixed $words
  213. */
  214. public static function remove_words ($words)
  215. {
  216. $words = is_array ($words) ? $words : array ($words);
  217. self::$remove_list = array_merge (self::$remove_list, $words);
  218. }
  219. /**
  220. * Transliterates characters to their ASCII equivalents.
  221. * $language specifies a priority for a specific language.
  222. * The latter is useful if languages have different rules for the same character.
  223. * @param string $text
  224. * @param string $language
  225. * @return string
  226. */
  227. public static function downcode ($text, $language = "")
  228. {
  229. self::init ($language);
  230. if (preg_match_all (self::$regex, $text, $matches)) {
  231. for ($i = 0; $i < count ($matches[0]); $i++) {
  232. $char = $matches[0][$i];
  233. if (isset (self::$map[$char])) {
  234. $text = str_replace ($char, self::$map[$char], $text);
  235. }
  236. }
  237. }
  238. return $text;
  239. }
  240. /**
  241. * Filters a string, e.g., "Petty theft" to "petty-theft"
  242. * @param string $text The text to return filtered
  243. * @param int $length The length (after filtering) of the string to be returned
  244. * @param string $language The transliteration language, passed down to downcode()
  245. * @param bool $file_name Whether there should be and additional filter considering this is a filename
  246. * @param bool $use_remove_list Whether you want to remove specific elements previously set in self::$remove_list
  247. * @param bool $lower_case Whether you want the filter to maintain casing or lowercase everything (default)
  248. * @param bool $treat_underscore_as_space Treat underscore as space, so it will replaced with "-"
  249. * @return string
  250. */
  251. public static function filter ($text, $length = 60, $language = "", $file_name = false, $use_remove_list = true, $lower_case = true, $treat_underscore_as_space = true)
  252. {
  253. $text = self::downcode ($text,$language);
  254. if ($use_remove_list) {
  255. // remove all these words from the string before urlifying
  256. $text = preg_replace ('/\b(' . join ('|', self::$remove_list) . ')\b/i', '', $text);
  257. }
  258. // if downcode doesn't hit, the char will be stripped here
  259. $remove_pattern = ($file_name) ? '/[^_\-.\-a-zA-Z0-9\s]/u' : '/[^\s_\-a-zA-Z0-9]/u';
  260. $text = preg_replace ($remove_pattern, '', $text); // remove unneeded chars
  261. if ($treat_underscore_as_space) {
  262. $text = str_replace ('_', ' ', $text); // treat underscores as spaces
  263. }
  264. $text = preg_replace ('/^\s+|\s+$/u', '', $text); // trim leading/trailing spaces
  265. $text = preg_replace ('/[-\s]+/u', '-', $text); // convert spaces to hyphens
  266. if ($lower_case) {
  267. $text = strtolower ($text); // convert to lowercase
  268. }
  269. return trim (substr ($text, 0, $length), '-'); // trim to first $length chars
  270. }
  271. /**
  272. * Alias of `URLify::downcode()`.
  273. */
  274. public static function transliterate ($text)
  275. {
  276. return self::downcode ($text);
  277. }
  278. }