PHP Classes
elePHPant
Icontem

File: README.md

Recommend this page to a friend!
  Classes of Lars Moelleken  >  PHP URLify  >  README.md  >  Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Documentation
Class: PHP URLify
Transliterate non-ASCII characters
Author: By
Last change: [!]: re-added licence from the "Django"-project v2
Date: 10 months ago
Size: 4,379 bytes
 

Contents

Class file image Download

Build Status Coverage Status codecov.io Scrutinizer Code Quality SensioLabsInsight Dependency Status Latest Stable Version Total Downloads Latest Unstable Version PHP 7 ready License

URLify for PHP

A PHP port of URLify.js from the Django project + str_transliterate from "Portable UTF-8".

  • https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
  • https://github.com/voku/portable-utf8

Handles symbols from many languages via an matching-array and others via "str_transliterate".

Install via "composer require"

composer require voku/urlify

Usage:

namespace: "voku\helper\URLify"

To generate slugs for URLs:

echo URLify::filter(' J\'étudie le français ');
// "J-etudie-le-francais"

echo URLify::filter('Lo siento, no hablo español.');
// "Lo-siento-no-hablo-espanol"

To generate slugs for file names:

echo URLify::filter('????.jpg', 60, '', true);
// "foto.jpg"

To simply transliterate characters:

echo URLify::downcode('J\'étudie le français');
// "J'etudie le francais"

echo URLify::downcode('Lo siento, no hablo español.');
// "Lo siento, no hablo espanol."

/Or use transliterate() alias:/

echo URLify::transliterate('Lo siento, no hablo español.');
// "Lo siento, no hablo espanol."

To extend the character list:

URLify::add_chars(array(
  '¿' => '?', '®' => '(r)', '¼' => '1/4',
  '¼' => '1/2', '¾' => '3/4', '¶' => 'P'
));

echo URLify::downcode('¿ ® ¼ ¼ ¾ ¶');
// "? (r) 1/2 1/2 3/4 P"

To extend or replace the default replacing list:

URLify::add_array_to_seperator(array(
  "/®/"
));

echo URLify::filter('¿ ® ¼ ¼ ¾ ¶');
// "14-14-34-P"

To extend the list of words to remove for one language:

URLify::remove_words(array('remove', 'these', 'too'), 'de');

To prioritize a certain language map:

echo URLify::filter(' Ägypten und Österreich besitzen wie üblich ein Übermaß an ähnlich öligen Attachés ', 60, 'de');
// "Aegypten-und-Oesterreich-besitzen-wie-ueblich-ein-Uebermass-aehnlich-oeligen-Attaches"
   
echo URLify::filter('Ca?alo?lu, çal??t???, müjde, laz?m, mahkûm', 60, 'tr');
// "Cagaloglu-calistigi-mujde-lazim-mahkum"

Please note that the "ü" is transliterated to "ue" in the first case, whereas it results in a simple "u" in the latter.

Available languages

  • Arabic: 'ar'
  • Austrian (German): 'de_at'
  • Austrian (French): 'fr_at'
  • Azerbaijani: 'az'
  • Bulgarian: 'bg'
  • Burmese: 'by'
  • Croatian: 'hr'
  • Czech: 'cs'
  • Danish: 'da'
  • English: 'en'
  • Esperanto: 'eo'
  • Estonian: 'et'
  • Finnish: 'fi'
  • French: 'fr'
  • Georgian: 'ka'
  • German: 'de'
  • Greek: 'el'
  • Hindi: 'hi'
  • Hungarian: 'hu'
  • Latvian: 'lv'
  • Lithuanian: 'lt'
  • Norwegian: 'no'
  • Polish: 'pl'
  • Romanian: 'ro'
  • Russian: 'ru'
  • Serbian: 'sr'
  • Swedish: 'sv'
  • Switzerland (German): 'de_ch'
  • Switzerland (French): 'fr_ch'
  • Turkish: 'tr'
  • Ukrainian: 'uk'
  • Vietnamese: 'vn'