PHPackages                             cocur/slugify - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. cocur/slugify

ActiveLibrary[Templating &amp; Views](/categories/templating)

cocur/slugify
=============

Converts a string into a slug.

v4.7.1(5mo ago)2.9k66.7M↓12.6%246[20 issues](https://github.com/cocur/slugify/issues)[11 PRs](https://github.com/cocur/slugify/pulls)20MITPHPPHP ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0 || ~8.5.0CI failing

Since Mar 23Pushed 5mo ago40 watchersCompare

[ Source](https://github.com/cocur/slugify)[ Packagist](https://packagist.org/packages/cocur/slugify)[ RSS](/packages/cocur-slugify/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (16)Versions (73)Used By (20)

cocur/slugify
=============

[](#cocurslugify)

> Converts a string into a slug.

[![Build Status](https://camo.githubusercontent.com/8c84a7d7052966098c5f3220c2c22c6ad949b07ba5b2a27dfd35ec2ed409c842/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6375722f736c75676966792e7376673f7374796c653d666c6174)](https://travis-ci.org/cocur/slugify)[![Windows Build status](https://camo.githubusercontent.com/65cbc042b2afe201d06226261b727fdeb1e8bf142a6357716e0a88b8ab3f5050/68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f397976343938666636316279703734323f7376673d74727565)](https://ci.appveyor.com/project/florianeckerstorfer/slugify)[![Scrutinizer Quality Score](https://camo.githubusercontent.com/c7138b8b6b0290ce0495de2162b4f4f4493ac6269510857de487dd91d9a29d41/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f636f6375722f736c75676966792e7376673f7374796c653d666c6174)](https://scrutinizer-ci.com/g/cocur/slugify/)[![Code Coverage](https://camo.githubusercontent.com/4b4a55f430b5f7d6484326e607a8e13b3510362d1f08dbfe879e135409b3235a/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f636f6375722f736c75676966792f6261646765732f636f7665726167652e706e673f623d6d6173746572267374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/cocur/slugify/?branch=master)

[![Latest Release](https://camo.githubusercontent.com/7edb5bf93c010f594b5cf13539704d3e47a2eba7bd26d1dbafc9127d8f34e7f3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f636f6375722f736c75676966792e737667)](https://packagist.org/packages/cocur/slugify)[![MIT License](https://camo.githubusercontent.com/b5221c1fb91e631be4fe85ee4b5eef9022a58dc812246629096bb2bd907e2023/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f636f6375722f736c75676966792e737667)](http://opensource.org/licenses/MIT)[![Total Downloads](https://camo.githubusercontent.com/10d947aea071636211702a32af69d7e1fb5105013ff6edde51394ffd2b87fe07/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f636f6375722f736c75676966792e737667)](https://packagist.org/packages/cocur/slugify)

Developed by [Florian Eckerstorfer](https://florian.ec) in Vienna, Europe with the help of [many great contributors](https://github.com/cocur/slugify/graphs/contributors).

Features
--------

[](#features)

- Removes all special characters from a string.
- Provides custom replacements for Arabic, Austrian, Azerbaijani, Brazilian Portuguese, Bulgarian, Burmese, Chinese, Croatian, Czech, Esperanto, Estonian, Finnish, French, Georgian, German, Greek, Hindi, Hungarian, Italian, Latvian, Lithuanian, Macedonian, Norwegian, Polish, Romanian, Russian, Serbian, Spanish, Swedish, Turkish, Ukrainian, Vietnamese and Yiddish special characters. Instead of removing these characters, Slugify approximates them (e.g., `ae` replaces `ä`).
- No external dependencies.
- PSR-4 compatible.
- Compatible with PHP 8.0 - 8.5.
- Integrations for [Symfony (3, 4 and 5)](http://symfony.com), [Laravel](http://laravel.com), [Twig (2 and 3)](http://twig.sensiolabs.org), [Zend Framework 2](http://framework.zend.com/), [Nette Framework](http://nette.org/), [Latte](http://latte.nette.org/) and [Plum](https://github.com/plumphp/plum).

Installation
------------

[](#installation)

You can install Slugify through [Composer](https://getcomposer.org):

```
composer require cocur/slugify
```

Slugify requires the Multibyte String extension from PHP. Typically you can use the configure option `--enable-mbstring` while compiling PHP. More information can be found in the [PHP documentation](http://php.net/manual/en/mbstring.installation.php).

Further steps may be needed for [integrations](#integrations).

Usage
-----

[](#usage)

Generate a slug:

```
use Cocur\Slugify\Slugify;

$slugify = new Slugify();
echo $slugify->slugify("Hello World!"); // hello-world
```

You can also change the separator used by `Slugify`:

```
echo $slugify->slugify("Hello World!", "_"); // hello_world
```

The library also contains `Cocur\Slugify\SlugifyInterface`. Use this interface whenever you need to type hint an instance of `Slugify`.

To add additional transliteration rules you can use the `addRule()` method.

```
$slugify->addRule("i", "ey");
echo $slugify->slugify("Hi"); // hey
```

### Rulesets

[](#rulesets)

Many of the transliterations rules used in Slugify are specific to a language. These rules are therefore categorized using rulesets. Rules for the most popular are activated by default in a specific order. You can change which rulesets are activated and the order in which they are activated. The order is important when there are conflicting rules in different languages. For example, in German `ä` is transliterated with `ae`, in Turkish the correct transliteration is `a`. By default the German transliteration is used since German is used more often on the internet. If you want to use prefer the Turkish transliteration you have to possibilities. You can activate it after creating the constructor:

```
$slugify = new Slugify();
$slugify->slugify("ä"); // -> "ae"
$slugify->activateRuleSet("turkish");
$slugify->slugify("ä"); // -> "a"
```

An alternative way would be to pass the rulesets and their order to the constructor.

```
$slugify = new Slugify(["rulesets" => ["default", "turkish"]]);
$slugify->slugify("ä"); // -> "a"
```

You can find a list of the available rulesets in [Resources/rules](https://github.com/cocur/slugify/tree/master/Resources/rules).

### More options

[](#more-options)

The constructor takes an options array, you have already seen the `rulesets` options above. You can also change the regular expression that is used to replace characters with the separator.

```
$slugify = new Slugify(["regexp" => "/([^A-Za-z0-9]|-)+/"]);
```

*(The regular expression used in the example above is the default one.)*

By default Slugify will convert the slug to lowercase. If you want to preserve the case of the string you can set the `lowercase` option to false.

```
$slugify = new Slugify(["lowercase" => false]);
$slugify->slugify("Hello World"); // -> "Hello-World"
```

Lowercasing is done before using the regular expression. If you want to keep the lowercasing behavior but your regular expression needs to match uppercase letters, you can set the `lowercase_after_regexp` option to `true`.

```
$slugify = new Slugify([
    "regexp" => "/(?
