Skip to content

Commit

Permalink
Merge pull request #62 from voku/portable_ascii
Browse files Browse the repository at this point in the history
[+]: use "voku/portable-ascii"
  • Loading branch information
jbroadway authored Nov 22, 2019
2 parents ced8f6c + 8cc59fa commit 91eb939
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 213 deletions.
254 changes: 47 additions & 207 deletions URLify.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,207 +18,39 @@
*/
class URLify
{
public static $maps = array (
'de' => array ( /* German */
'Ä' => 'Ae', 'Ö' => 'Oe', 'Ü' => 'Ue', 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'ß' => 'ss',
'' => 'SS'
),
'latin' => array (
'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A', 'Å' => 'A','Ă' => 'A', 'Æ' => 'AE', 'Ç' =>
'C', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O', 'Ö' =>
'O', 'Ő' => 'O', 'Ø' => 'O', 'Œ' => 'OE' ,'Ș' => 'S','Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' =>
'a', 'å' => 'a', 'ă' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e', 'ê' => 'e', 'ë' => 'e',
'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' =>
'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o', 'œ' => 'oe', 'ș' => 's', 'ț' => 't', 'ù' => 'u', 'ú' => 'u',
'û' => 'u', 'ü' => 'u', 'ű' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y'
),
'latin_symbols' => array (
'©' => '(c)'
),
'el' => array ( /* Greek */
'α' => 'a', 'β' => 'b', 'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h', 'θ' => '8',
'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm', 'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p',
'ρ' => 'r', 'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x', 'ψ' => 'ps', 'ω' => 'w',
'ά' => 'a', 'έ' => 'e', 'ί' => 'i', 'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E', 'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8',
'Ι' => 'I', 'Κ' => 'K', 'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O', 'Π' => 'P',
'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y', 'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W',
'Ά' => 'A', 'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H', 'Ώ' => 'W', 'Ϊ' => 'I',
'Ϋ' => 'Y'
),
'tr' => array ( /* Turkish */
'ş' => 's', 'Ş' => 'S', 'ı' => 'i', 'İ' => 'I', 'ç' => 'c', 'Ç' => 'C', 'ü' => 'u', 'Ü' => 'U',
'ö' => 'o', 'Ö' => 'O', 'ğ' => 'g', 'Ğ' => 'G'
),
'bg' => array( /* Bulgarian */
'Щ' => 'Sht', 'Ш' => 'Sh', 'Ч' => 'Ch', 'Ц' => 'C', 'Ю' => 'Yu', 'Я' => 'Ya',
'Ж' => 'J', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
'Е' => 'E', 'З' => 'Z', 'И' => 'I', 'Й' => 'Y', 'К' => 'K', 'Л' => 'L',
'М' => 'M', 'Н' => 'N', 'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S',
'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ь' => '', 'Ъ' => 'A',
'щ' => 'sht', 'ш' => 'sh', 'ч' => 'ch', 'ц' => 'c', 'ю' => 'yu', 'я' => 'ya',
'ж' => 'j', 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
'е' => 'e', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ь' => '', 'ъ' => 'a'
),
'ru' => array ( /* Russian */
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh',
'з' => 'z', 'и' => 'i', 'й' => 'i', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o',
'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c',
'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
'я' => 'ya',
'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh',
'З' => 'Z', 'И' => 'I', 'Й' => 'I', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C',
'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'Yu',
'Я' => 'Ya',
'' => ''
),
'uk' => array ( /* Ukrainian */
'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye', 'і' => 'i', 'ї' => 'yi', 'ґ' => 'g'
),
'kk' => array ( /* Kazakh */
'Ә' => 'A', 'Ғ' => 'G', 'Қ' => 'Q', 'Ң' => 'N', 'Ө' => 'O', 'Ұ' => 'U', 'Ү' => 'U', 'Һ' => 'H',
'ә' => 'a', 'ғ' => 'g', 'қ' => 'q', 'ң' => 'n', 'ө' => 'o', 'ұ' => 'u', 'ү' => 'u', 'һ' => 'h',
),
'cs' => array ( /* Czech */
'č' => 'c', 'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's', 'ť' => 't', 'ů' => 'u',
'ž' => 'z', 'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R', 'Š' => 'S', 'Ť' => 'T',
'Ů' => 'U', 'Ž' => 'Z'
),
'pl' => array ( /* Polish */
'ą' => 'a', 'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o', 'ś' => 's', 'ź' => 'z',
'ż' => 'z', 'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N', 'Ó' => 'O', 'Ś' => 'S',
'Ź' => 'Z', 'Ż' => 'Z'
),
'ro' => array ( /* Romanian */
'ă' => 'a', 'â' => 'a', 'î' => 'i', 'ș' => 's', 'ț' => 't', 'Ţ' => 'T', 'ţ' => 't'
),
'lv' => array ( /* Latvian */
'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g', 'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n',
'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i',
'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'u', 'Ž' => 'Z'
),
'lt' => array ( /* Lithuanian */
'ą' => 'a', 'č' => 'c', 'ę' => 'e', 'ė' => 'e', 'į' => 'i', 'š' => 's', 'ų' => 'u', 'ū' => 'u', 'ž' => 'z',
'Ą' => 'A', 'Č' => 'C', 'Ę' => 'E', 'Ė' => 'E', 'Į' => 'I', 'Š' => 'S', 'Ų' => 'U', 'Ū' => 'U', 'Ž' => 'Z'
),
'vi' => array ( /* Vietnamese */
'Á' => 'A', 'À' => 'A', '' => 'A', 'Ã' => 'A', '' => 'A', 'Ă' => 'A', '' => 'A', '' => 'A', '' => 'A', '' => 'A', '' => 'A', 'Â' => 'A', '' => 'A', '' => 'A', '' => 'A', '' => 'A', '' => 'A',
'á' => 'a', 'à' => 'a', '' => 'a', 'ã' => 'a', '' => 'a', 'ă' => 'a', '' => 'a', '' => 'a', '' => 'a', '' => 'a', '' => 'a', 'â' => 'a', '' => 'a', '' => 'a', '' => 'a', '' => 'a', '' => 'a',
'É' => 'E', 'È' => 'E', '' => 'E', '' => 'E', '' => 'E', 'Ê' => 'E', '' => 'E', '' => 'E', '' => 'E', '' => 'E', '' => 'E',
'é' => 'e', 'è' => 'e', '' => 'e', '' => 'e', '' => 'e', 'ê' => 'e', 'ế' => 'e', '' => 'e', '' => 'e', '' => 'e', '' => 'e',
'Í' => 'I', 'Ì' => 'I', '' => 'I', 'Ĩ' => 'I', '' => 'I', 'í' => 'i', 'ì' => 'i', '' => 'i', 'ĩ' => 'i', '' => 'i',
'Ó' => 'O', 'Ò' => 'O', '' => 'O', 'Õ' => 'O', '' => 'O', 'Ô' => 'O', '' => 'O', '' => 'O', '' => 'O', '' => 'O', '' => 'O', 'Ơ' => 'O', '' => 'O', '' => 'O', '' => 'O', '' => 'O', '' => 'O',
'ó' => 'o', 'ò' => 'o', '' => 'o', 'õ' => 'o', '' => 'o', 'ô' => 'o', '' => 'o', '' => 'o', '' => 'o', '' => 'o', '' => 'o', 'ơ' => 'o', '' => 'o', '' => 'o', '' => 'o', '' => 'o', '' => 'o',
'Ú' => 'U', 'Ù' => 'U', '' => 'U', 'Ũ' => 'U', '' => 'U', 'Ư' => 'U', '' => 'U', '' => 'U', '' => 'U', '' => 'U', '' => 'U',
'ú' => 'u', 'ù' => 'u', '' => 'u', 'ũ' => 'u', '' => 'u', 'ư' => 'u', '' => 'u', '' => 'u', '' => 'u', '' => 'u', '' => 'u',
'Ý' => 'Y', '' => 'Y', '' => 'Y', '' => 'Y', '' => 'Y', 'ý' => 'y', '' => 'y', '' => 'y', '' => 'y', '' => 'y',
'Đ' => 'D', 'đ' => 'd'
),
'ar' => array ( /* Arabic */
'أ' => 'a', 'ب' => 'b', 'ت' => 't', 'ث' => 'th', 'ج' => 'g', 'ح' => 'h', 'خ' => 'kh', 'د' => 'd',
'ذ' => 'th', 'ر' => 'r', 'ز' => 'z', 'س' => 's', 'ش' => 'sh', 'ص' => 's', 'ض' => 'd', 'ط' => 't',
'ظ' => 'th', 'ع' => 'aa', 'غ' => 'gh', 'ف' => 'f', 'ق' => 'k', 'ك' => 'k', 'ل' => 'l', 'م' => 'm',
'ن' => 'n', 'ه' => 'h', 'و' => 'o', 'ي' => 'y',
'ا' => 'a', 'إ' => 'a', 'آ' => 'a', 'ؤ' => 'o', 'ئ' => 'y', 'ء' => 'aa',
'٠' => '0', '١' => '1', '٢' => '2', '٣' => '3', '٤' => '4', '٥' => '5', '٦' => '6', '٧' => '7', '٨' => '8', '٩' => '9',
),
'fa' => array ( /* Persian */
'گ' => 'g', 'ژ' => 'j', 'پ' => 'p', 'چ' => 'ch', 'ی' => 'y', 'ک' => 'k',
'۰' => '0', '۱' => '1', '۲' => '2', '۳' => '3', '۴' => '4', '۵' => '5', '۶' => '6', '۷' => '7', '۸' => '8', '۹' => '9',
),
'sr' => array ( /* Serbian */
'ђ' => 'dj', 'ј' => 'j', 'љ' => 'lj', 'њ' => 'nj', 'ћ' => 'c', 'џ' => 'dz', 'đ' => 'dj',
'Ђ' => 'Dj', 'Ј' => 'j', 'Љ' => 'Lj', 'Њ' => 'Nj', 'Ћ' => 'C', 'Џ' => 'Dz', 'Đ' => 'Dj'
),
'az' => array ( /* Azerbaijani */
'ç' => 'c', 'ə' => 'e', 'ğ' => 'g', 'ı' => 'i', 'ö' => 'o', 'ş' => 's', 'ü' => 'u',
'Ç' => 'C', 'Ə' => 'E', 'Ğ' => 'G', 'İ' => 'I', 'Ö' => 'O', 'Ş' => 'S', 'Ü' => 'U'
),
'sk' => array ( /* Slovak */
'ĺ' => 'l', 'ľ' => 'l', 'ŕ' => 'r'
)
);

/**
* List of words to remove from URLs.
*/
public static $remove_list = array (
'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from',
'is', 'in', 'into', 'like', 'of', 'off', 'on', 'onto', 'per',
'since', 'than', 'the', 'this', 'that', 'to', 'up', 'via',
'with'
);

/**
* The character map.
*/
private static $map = array ();

/**
* The character list as a string.
*/
private static $chars = '';

/**
* The character list as a regular expression.
*/
private static $regex = '';

/**
* The current language
*/
private static $language = '';

/**
* Initializes the character map.
* @param string $language
*/
private static function init ($language = "")
{
if (count (self::$map) > 0 && (($language == "") || ($language == self::$language))) {
return;
}

/* Is a specific map associated with $language ? */
if (isset(self::$maps[$language]) && is_array(self::$maps[$language])) {
/* Move this map to end. This means it will have priority over others */
$m = self::$maps[$language];
unset(self::$maps[$language]);
self::$maps[$language] = $m;
}
/* Reset static vars */
self::$language = $language;
self::$map = array();
self::$chars = '';

foreach (self::$maps as $map) {
foreach ($map as $orig => $conv) {
self::$map[$orig] = $conv;
self::$chars .= $orig;
}
}

self::$regex = '/[' . preg_quote(self::$chars, '/') . ']/u';
}
/**
* The language-mapping array.
*
* ISO 639-1 codes: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
*
* @var array
*/
public static $maps = [];

/**
* List of words to remove from URLs.
*/
public static $remove_list = array (
'a', 'an', 'as', 'at', 'before', 'but', 'by', 'for', 'from',
'is', 'in', 'into', 'like', 'of', 'off', 'on', 'onto', 'per',
'since', 'than', 'the', 'this', 'that', 'to', 'up', 'via',
'with'
);

/**
* Add new characters to the list. `$map` should be a hash.
* @param array $map
* @param string|null $language
*/
public static function add_chars ($map)
public static function add_chars ($map, string $language = null)
{
if (! is_array ($map)) {
throw new LogicException ('$map must be an associative array.');
}
self::$maps[] = $map;
self::$map = array ();
self::$chars = '';
$language_key = $language ?? uniqid('urlify', true);

if (isset(self::$maps[$language_key])) {
self::$maps[$language_key] = array_merge($map, self::$maps[$language_key]);
} else {
self::$maps[$language_key] = $map;
}
}

/**
Expand All @@ -242,17 +74,25 @@ public static function remove_words ($words)
*/
public static function downcode ($text, $language = "")
{
self::init ($language);

if (preg_match_all (self::$regex, $text, $matches)) {
for ($i = 0; $i < count ($matches[0]); $i++) {
$char = $matches[0][$i];
if (isset (self::$map[$char])) {
$text = str_replace ($char, self::$map[$char], $text);
}
}
}
return $text;
foreach (self::$maps as $mapsInner) {
foreach ($mapsInner as $orig => $replace) {
$text = str_replace($orig, $replace, $text);
}
}

$langSpecific = \voku\helper\ASCII::charsArrayWithOneLanguage($language, true);
if (!empty($langSpecific)) {
$text = str_replace(
$langSpecific['orig'],
$langSpecific['replace'],
$text
);
}
foreach (\voku\helper\ASCII::charsArrayWithMultiLanguageValues(true) as $replace => $orig) {
$text = str_replace($orig, $replace, $text);
}

return $text;
}

/**
Expand All @@ -272,7 +112,7 @@ public static function filter ($text, $length = 60, $language = "", $file_name =

if ($use_remove_list) {
// remove all these words from the string before urlifying
$text = preg_replace ('/\b(' . join ('|', self::$remove_list) . ')\b/i', '', $text);
$text = preg_replace ('/\b(' . implode ('|', self::$remove_list) . ')\b/i', '', $text);
}

// if downcode doesn't hit, the char will be stripped here
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=7.0.0",
"voku/portable-ascii": "^1.3"
},
"require-dev": {
"phpunit/phpunit": "^4.8.36 || ^5.7 || ^6.5"
"phpunit/phpunit": "~6.0 || ~7.0"
},
"autoload": {
"psr-0": { "URLify": "" }
Expand Down
8 changes: 4 additions & 4 deletions tests/URLifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ class URLifyTest extends TestCase {
function test_downcode () {
$this->assertEquals (' J\'etudie le francais ', URLify::downcode (' J\'étudie le français '));
$this->assertEquals ('Lo siento, no hablo espanol.', URLify::downcode ('Lo siento, no hablo español.'));
$this->assertEquals ('F3PWS', URLify::downcode ('ΦΞΠΏΣ'));
$this->assertEquals ('FKsPWS', URLify::downcode ('ΦΞΠΏΣ'));
$this->assertEquals ('foo-bar', URLify::filter ('_foo_bar_'));
}

function test_filter () {
$this->assertEquals ('jetudie-le-francais', URLify::filter (' J\'étudie le français '));
$this->assertEquals ('lo-siento-no-hablo-espanol', URLify::filter ('Lo siento, no hablo español.'));
$this->assertEquals ('f3pws', URLify::filter ('ΦΞΠΏΣ'));
$this->assertEquals ('fkspws', URLify::filter ('ΦΞΠΏΣ'));
$this->assertEquals ('', URLify::filter('大般若經'));
$this->assertEquals ('test-.txt', URLify::filter('test-大般若經.txt', 60, "", $file_name = true));
$this->assertEquals ('yakrhy-ltoytr', URLify::filter('ياكرهي لتويتر'));
$this->assertEquals ('saaat-25', URLify::filter('ساعت ۲۵'));
$this->assertEquals ('saaat', URLify::filter('ساعت ۲۵'));
$this->assertEquals ('foto.jpg', URLify::filter ('фото.jpg', 60, "", $file_name = true));
// priorization of language-specific maps
$this->assertEquals ('aouaou', URLify::filter ('ÄÖÜäöü',60,"tr"));
Expand All @@ -31,7 +31,7 @@ function test_filter () {
}

function test_add_chars () {
$this->assertEquals ('¿ ® ¼ ¼ ¾ ¶', URLify::downcode ('¿ ® ¼ ¼ ¾ ¶'));
$this->assertEquals ('¿ (r) ¼ ¼ ¾ ¶', URLify::downcode ('¿ ® ¼ ¼ ¾ ¶'));
URLify::add_chars (array (
'¿' => '?', '®' => '(r)', '¼' => '1/4',
'¼' => '1/2', '¾' => '3/4', '' => 'P'
Expand Down

0 comments on commit 91eb939

Please sign in to comment.