Skip to content

Commit

Permalink
Added URLify::slug() with simplified parameters for slug generation
Browse files Browse the repository at this point in the history
  • Loading branch information
lux committed Jun 14, 2020
1 parent fa8f433 commit 9b227e8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# URLify for PHP [![Build Status](https://travis-ci.org/jbroadway/urlify.png)](https://travis-ci.org/jbroadway/urlify)

A PHP port of [URLify.js](https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js)
from the Django project. Handles symbols from Latin languages as well as Arabic, Azerbaijani, Czech, German, Greek, Kazakh,
Latvian, Lithuanian, Persian, Polish, Romanian, Bulgarian, Russian, Serbian, Turkish, Ukrainian, Vietnamese and Slovak. Symbols it cannot
transliterate it will simply omit.
A fast PHP slug generator and transliteration library, started as a PHP port of
[URLify.js](https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js)
from the Django project.

Handles symbols from latin languages, Arabic, Azerbaijani, Bulgarian, Burmese, Croatian, Czech, Danish, Esperanto,
Estonian, Finnish, French, Switzerland (French), Austrian (French), Georgian, German, Switzerland (German),
Austrian (German), Greek, Hindi, Kazakh, Latvian, Lithuanian, Norwegian, Persian, Polish, Romanian, Russian, Swedish,
Serbian, Slovak, Turkish, Ukrainian and Vietnamese, and many other via `ASCII::to_transliterate()`.

Symbols it cannot transliterate it can omit or replace with a specified character.

## Installation

Expand All @@ -26,10 +32,10 @@ To generate slugs for URLs:
```php
<?php

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

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

Expand Down
22 changes: 21 additions & 1 deletion URLify.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

/**
* A PHP port of URLify.js from the Django project + fallback via "Portable ASCII".
* A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js
* from the Django project + fallback via "Portable ASCII".
*
* - https://github.com/django/django/blob/master/django/contrib/admin/static/admin/js/urlify.js
* - https://github.com/voku/portable-ascii
Expand Down Expand Up @@ -126,6 +127,25 @@ public static function downcode(
false
);
}

/**
* Convert a String to URL slug. Wraps <strong>filter()</strong> with a simpler
* set of defaults for typical usage in generating blog post slugs.
*
* @param string $string <p>The text you want to convert.</p>
* @param int $maxLength <p>Max. length of the output string, set to "0" (zero) to
* disable it</p>
* @param string $separator <p>Define a new separator for the words.</p>
* @param string $language <p>The language you want to convert to.</p>
*/
public static function slug(
string $string,
int $maxLength = 200,
string $separator = '-',
string $language = 'en'
): string {
return self::filter ($string, $maxLength, $language, false, false, true, $separator);
}

/**
* Convert a String to URL.
Expand Down

0 comments on commit 9b227e8

Please sign in to comment.