Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.

Overview

cocur/slugify

Converts a string into a slug.

Build Status Windows Build status Scrutinizer Quality Score Code Coverage

Latest Release MIT License Total Downloads

Developed by Florian Eckerstorfer in Vienna, Europe with the help of many great contributors.

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 and Vietnamese special characters. Instead of removing these characters, Slugify approximates them (e.g., ae replaces ä).
  • No external dependencies.
  • PSR-4 compatible.
  • Compatible with PHP >= 7.
  • Integrations for Symfony (3, 4 and 5), Laravel, Twig (2 and 3), Zend Framework 2, Nette Framework, Latte and Plum.

Installation

You can install Slugify through Composer:

$ 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.

Further steps may be needed for integrations.

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

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.

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' => '/(?<=[[:^upper:]])(?=[[:upper:]])/',
    'lowercase_after_regexp' => false,
]);
$slugify->slugify('FooBar'); // -> "foo-bar"

By default Slugify will use dashes as separators. If you want to use a different default separator, you can set the separator option.

$slugify = new Slugify(['separator' => '_']);
$slugify->slugify('Hello World'); // -> "hello_world"

By default Slugify will remove leading and trailing separators before returning the slug. If you do not want the slug to be trimmed you can set the trim option to false.

$slugify = new Slugify(['trim' => false]);
$slugify->slugify('Hello World '); // -> "hello-world-"

Changing options on the fly

You can overwrite any of the above options on the fly by passing an options array as second argument to the slugify() method. For example:

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

You can also modify the separator this way:

$slugify = new Slugify();
$slugify->slugify('Hello World', ['separator' => '_']); // -> "hello_world"

You can even activate a custom ruleset without touching the default rules:

$slugify = new Slugify();
$slugify->slugify('für', ['ruleset' => 'turkish']); // -> "fur"
$slugify->slugify('für'); // -> "fuer"

Contributing

We really appreciate if you report bugs and errors in the transliteration, especially if you are a native speaker of the language and question. Feel free to ask for additional languages in the issues, but please note that the maintainer of this repository does not speak all languages. If you can provide a Pull Request with rules for a new language or extend the rules for an existing language that would be amazing.

To add a new language you need to:

  1. Create a [language].json in Resources/rules
  2. If you believe the language should be a default ruleset you can add the language to Cocur\Slugify\Slugify::$options. If you add the language there all existing tests still have to pass
  3. Run php bin/generate-default.php
  4. Add tests for the language in tests/SlugifyTest.php. If the language is in the default ruleset add your test cases to defaultRuleProvider(), otherwise to customRulesProvider().

Submit PR. Thank you very much. 💚

Code of Conduct

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

The full Code of Conduct can be found here.

This project is no place for hate. If you have any problems please contact Florian: [email protected] ✌🏻 🏳️‍🌈

Further information

Integrations

Symfony

Slugify contains a Symfony bundle and service definition that allow you to use it as a service in your Symfony application. The code resides in Cocur\Slugify\Bridge\Symfony\CocurSlugifyBundle and you only need to activate it:

Symfony 2

Support for Symfony 2 has been dropped in Slugify 4.0.0, use cocur/slugify@3.

Symfony 3

// app/AppKernel.php

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Cocur\Slugify\Bridge\Symfony\CocurSlugifyBundle(),
        );
    }
}

Symfony >= 4

// config/bundles.php

return [
    // ...
    Cocur\Slugify\Bridge\Symfony\CocurSlugifyBundle::class => ['all' => true],
];

You can now use the cocur_slugify service everywhere in your application, for example, in your controller:

$slug = $this->get('cocur_slugify')->slugify('Hello World!');

The bundle also provides an alias slugify for the cocur_slugify service:

$slug = $this->get('slugify')->slugify('Hello World!');

If you use autowire (Symfony >=3.3), you can inject it into your services like this:

public function __construct(\Cocur\Slugify\SlugifyInterface $slugify)

Symfony Configuration

You can set the following configuration settings in config.yml (Symfony 2-3) or config/packages/cocur_slugify.yaml (Symfony 4) to adjust the slugify service:

cocur_slugify:
    lowercase: false # or true
    separator: '-' # any string
    # regexp: <string>
    rulesets: ['austrian'] # List of rulesets: https://github.com/cocur/slugify/tree/master/Resources/rules

Twig

If you use the Symfony framework with Twig you can use the Twig filter slugify in your templates after you have setup Symfony integrations (see above).

{{ 'Hällo Wörld'|slugify }}

If you use Twig outside of the Symfony framework you first need to add the extension to your environment:

use Cocur\Slugify\Bridge\Twig\SlugifyExtension;
use Cocur\Slugify\Slugify;

$twig = new Twig_Environment($loader);
$twig->addExtension(new SlugifyExtension(Slugify::create()));

To use the Twig filter with TwigBridge for Laravel, you'll need to add the Slugify extension using a closure:

// laravel/app/config/packages/rcrowe/twigbridge/config.php

'extensions' => array(
    //...
    function () {
        return new \Cocur\Slugify\Bridge\Twig\SlugifyExtension(\Cocur\Slugify\Slugify::create());
    },
),

You can find more information about registering extensions in the Twig documentation.

Mustache.php

We don't need an additional integration to use Slugify in Mustache.php. If you want to use Slugify in Mustache, just add a helper:

use Cocur\Slugify\Slugify;

$mustache = new Mustache_Engine(array(
    // ...
    'helpers' => array('slugify' => function($string, $separator = null) {
        return Slugify::create()->slugify($string, $separator);
    }),
));

Laravel

Slugify also provides a service provider to integrate into Laravel (versions 4.1 and later).

In your Laravel project's app/config/app.php file, add the service provider into the "providers" array:

'providers' => array(
    "Cocur\Slugify\Bridge\Laravel\SlugifyServiceProvider",
)

And add the facade into the "aliases" array:

'aliases' => array(
    "Slugify" => "Cocur\Slugify\Bridge\Laravel\SlugifyFacade",
)

You can then use the Slugify::slugify() method in your controllers:

$url = Slugify::slugify('welcome to the homepage');

Zend Framework 2

Slugify can be easely used in Zend Framework 2 applications. Included bridge provides a service and a view helper already registered for you.

Just enable the module in your configuration like this.

return array(
    //...

    'modules' => array(
        'Application',
        'ZfcBase',
        'Cocur\Slugify\Bridge\ZF2' // <- Add this line
        //...
    )

    //...
);

After that you can retrieve the Cocur\Slugify\Slugify service (or the slugify alias) and generate a slug.

/** @var \Zend\ServiceManager\ServiceManager $sm */
$slugify = $sm->get('Cocur\Slugify\Slugify');
$slug = $slugify->slugify('Hällo Wörld');
$anotherSlug = $slugify->slugify('Hällo Wörld', '_');

In your view templates use the slugify helper to generate slugs.

<?php echo $this->slugify('Hällo Wörld') ?>
<?php echo $this->slugify('Hällo Wörld', '_') ?>

The service (which is also used in the view helper) can be customized by defining this configuration key.

return array(
    'cocur_slugify' => array(
        'reg_exp' => '/([^a-zA-Z0-9]|-)+/'
    )
);

Nette Framework

Slugify contains a Nette extension that allows you to use it as a service in your Nette application. You only need to register it in your config.neon:

# app/config/config.neon

extensions:
    slugify: Cocur\Slugify\Bridge\Nette\SlugifyExtension

You can now use the Cocur\Slugify\SlugifyInterface service everywhere in your application, for example in your presenter:

class MyPresenter extends \Nette\Application\UI\Presenter
{
    /** @var \Cocur\Slugify\SlugifyInterface @inject */
    public $slugify;

    public function renderDefault()
    {
        $this->template->hello = $this->slugify->slugify('Hällo Wörld');
    }
}

Latte

If you use the Nette Framework with it's native Latte templating engine, you can use the Latte filter slugify in your templates after you have setup Nette extension (see above).

{$hello|slugify}

If you use Latte outside of the Nette Framework you first need to add the filter to your engine:

use Cocur\Slugify\Bridge\Latte\SlugifyHelper;
use Cocur\Slugify\Slugify;
use Latte;

$latte = new Latte\Engine();
$latte->addFilter('slugify', array(new SlugifyHelper(Slugify::create()), 'slugify'));

Slim 3

Slugify does not need a specific bridge to work with Slim 3, just add the following configuration:

$container['view'] = function ($c) {
    $settings = $c->get('settings');
    $view = new \Slim\Views\Twig($settings['view']['template_path'], $settings['view']['twig']);
    $view->addExtension(new Slim\Views\TwigExtension($c->get('router'), $c->get('request')->getUri()));
    $view->addExtension(new Cocur\Slugify\Bridge\Twig\SlugifyExtension(Cocur\Slugify\Slugify::create()));
    return $view;
};

In a template you can use it like this:

<a href="/blog/{{ post.title|slugify }}">{{ post.title|raw }}</a></h5>

League

Slugify provides a service provider for use with league/container:

use Cocur\Slugify;
use League\Container;

/* @var Container\ContainerInterface $container */
$container->addServiceProvider(new Slugify\Bridge\League\SlugifyServiceProvider());

/* @var Slugify\Slugify $slugify */
$slugify = $container->get(Slugify\SlugifyInterface::class);

You can configure it by sharing the required options:

use Cocur\Slugify;
use League\Container;

/* @var Container\ContainerInterface $container */
$container->share('config.slugify.options', [
    'lowercase' => false,
    'rulesets' => [
        'default',
        'german',
    ],
]);

$container->addServiceProvider(new Slugify\Bridge\League\SlugifyServiceProvider());

/* @var Slugify\Slugify $slugify */
$slugify = $container->get(Slugify\SlugifyInterface::class);

You can configure which rule provider to use by sharing it:

use Cocur\Slugify;
use League\Container;

/* @var Container\ContainerInterface $container */
$container->share(Slugify\RuleProvider\RuleProviderInterface::class, function () {
    return new Slugify\RuleProvider\FileRuleProvider(__DIR__ . '/../../rules');
]);

$container->addServiceProvider(new Slugify\Bridge\League\SlugifyServiceProvider());

/* @var Slugify\Slugify $slugify */
$slugify = $container->get(Slugify\SlugifyInterface::class);

Change Log

Version 4.0 (14 December 2019)

Version 4 does not introduce new major features, but adds support for Symfony 4 and 5, Twig 3 and, most importantly, PHP 7.3 and 7.4.

Support for PHP 5, Twig 1 and Silex is dropped.

Version 3.2 (31 January 2019)

Version 3.1 (22 January 2018)

Version 3.0.1 (24 September 2017)

Version 3.0 (11 August 2017)

  • HHVM is no longer supported
  • Bugfix #165 Added missing French rules to DefaultRuleProvider (by gsouf)
  • #168 Add Persian rules (by mohammad6006)
  • Bugfix #169 Add missing getName() to Cocur\Slugify\Bridge\Twig\SlugifyExtension (by TomCan)
  • #172 Sort rules in DefaultRuleProvider alphabetically (by tbmatuka)
  • #174 Add Hungarian rules (by rviktor87)
  • #180 Add Brazilian Portuguese rules (by tallesairan)
  • Bugfix #181 Add missing French rules (by FabienPapet)

Version 2.5 (23 March 2017)

Version 2.4 (9 February 2017)

Version 2.3 (9 August 2016)

Version 2.2 (10 July 2016)

Version 2.1.1 (8 April 2016)

  • Do not activate Swedish rules by default (fixes broken v2.1 release)

Version 2.1.0 (8 April 2016)

Version 2.0.0 (24 February 2016)

Version 1.4.1 (11 February 2016)

  • #90 Replace bindShared with singleton in Laravel bridge (by sunspikes)

Version 1.4 (29 September 2015)

Version 1.3 (2 September 2015)

  • #70 Add missing superscript and subscript digits (by BlueM)
  • #71 Improve Greek language support (by kostaspt)
  • #72 Improve Silex integration (by CarsonF)
  • #73 Improve Russian language support (by akost)

Version 1.2 (2 July 2015)

Version 1.1 (18 March 2015)

Version 1.0 (26 November 2014)

No new features or bugfixes, but it's about time to pump Slugify to v1.0.

Version 0.11 (23 November 2014)

  • #49 Add Zend Framework 2 integration (by acelaya)

Version 0.10.3 (8 November 2014)

Version 0.10.2 (18 October 2014)

Version 0.10.1 (1 September 2014)

Version 0.10.0 (26 August 2014)

Version 0.9 (29 May 2014)

  • #28 Add Symfony2 service alias and make Twig extension private (by Kevin Bond)

Version 0.8 (18 April 2014)

  • #27 Add support for Arabic characters (by Davide Bellini)
  • Added some missing characters
  • Improved organisation of characters in Slugify class

Version 0.7 (4 April 2014)

This version introduces optional integrations into Symfony2, Silex and Twig. You can still use the library in any other framework. I decided to include these bridges because there exist integrations from other developers, but they use outdated versions of cocur/slugify. Including these small bridge classes in the library makes maintaining them a lot easier for me.

  • #23 Added Symfony2 service
  • #24 Added Twig extension
  • #25 Added Silex service provider

Version 0.6 (2 April 2014)

Version 0.5 (28 March 2014)

Version 0.4.1 (9 March 2014)

Version 0.4 (17 January 2014)

Nearly completely rewritten code, removes iconv support because the underlying library is broken. The code is now better and faster. Many thanks to Marchenko Alexandr.

Version 0.3 (12 January 2014)

  • #11 PSR-4 compatible (by mac2000)
  • #13 Added editorconfig (by mac2000)
  • #14 Return empty slug when input is empty and removed unused parameter (by mac2000)

Authors

Support for Chinese is adapted from jifei/Pinyin with permission.

Slugify is a project of Cocur. You can contact us on Twitter: @cocurco

Support

If you need support you can ask on Twitter (well, only if your question is short) or you can join our chat on Gitter.

Gitter

In case you want to support the development of Slugify you can help us with providing additional transliterations or inform us if a transliteration is wrong. We would highly appreciate it if you can send us directly a Pull Request on Github. If you have never contributed to a project on Github we are happy to help you. Just ask on Twitter or directly join our Gitter.

You always can help me (Florian, the original developer and maintainer) out by sending me an Euro or two.

License

The MIT License (MIT)

Copyright (c) 2012-2017 Florian Eckerstorfer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments
  • Turkish problem

    Turkish problem

    Hey,

    Seems like some Turkish letters fall into German:

    Türkiye Kupasi => tuerkiye-kupasi

    While it should be turkiye-kupasi. Same problem with ö which should fall into o.

    Not sure, but it seems Turkish should go to a ruleset.

    opened by lchachurski 16
  • Symfony 3.3 deprecation notice

    Symfony 3.3 deprecation notice

    Just a heads up:

    Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won't be supported in version 4.0. You should rename (or alias) the "cocur_slugify" service to "Cocur\Slugify\SlugifyInterface" instead.

    opened by pevdh 14
  • AppVeyor build is breaking

    AppVeyor build is breaking

    Has someone any experience with AppVeyor and can help me fix the build on Windows? It seems that there is a problem installing or configuring PHP on the machine breaks.

    Link to latest build on AppVeyor

    help wanted 
    opened by florianeckerstorfer 10
  • Feature/regexp

    Feature/regexp

    This allows the user to define the regular expression to be used to sanitize the slug. It can be set either in the constructor or by calling the method setRegExp. By default the regular expression is the same that was being used until now.

    opened by acelaya 10
  • Notice: Undefined index: french 500 Internal Server Error - ContextErrorException

    Notice: Undefined index: french 500 Internal Server Error - ContextErrorException

    $slugger = $this->container->get('cocur_slugify');
    $slugger->activateRuleSet('french');
    
    $test = $slugger->slugify("c'est un test ça alors, lol é è ça");
    

    SF2.8 php7.1

    question 
    opened by ibasaw 9
  • Allow to modify options without creating a new object

    Allow to modify options without creating a new object

    Right now we need to create a new object instance every time we e.g. need a different regexp. This is cumbersome when using the library as a Symfony service, because it would require to define one service per configuration.

    This PR adds methods to modify the options on the fly, so we can e.g. do this:

    $slugify = $container->get('slugify');
    $slugify->slugify('MY STRING', ['lowercase' => false]); // MY-STRING
    

    TODO

    • [x] Add the unit tests

    If you decide that you want to implement this, I'll happily add the unit tests.

    opened by leofeyer 9
  • slugify fail for long string of other language e.gMarathi,Hindi,kannada etc.....

    slugify fail for long string of other language e.gMarathi,Hindi,kannada etc.....

    for this msg 11:50AM - बांगलादेशमध्ये सलग तिसऱ्या वर्षी आशिया कप टी-ट्वेंटी स्पर्धेचे आयोजन करण्यात आले असून २४ फेब्रुवारी ते ६ मार्च दरम्यान ही स्पर्धा होणार आहे. 11:46AM - नगरमध्ये त्रिमूर्ती कॉलेजमधील १२ वीच्या विद्यार्थीनीची आत्महत्या, वर्गातच लावून घेतला गळफास 11:38AM - बँकॉकमधील नृत्यस्पर्धेसाठी मुख्यमंत्र्याकडून देण्यात आलेला ८ लाख रुपयांचा निधी कलावंतांनी परत दिला आहे.11:50AM - बांगलादेशमध्ये सलग तिसऱ्या वर्षी आशिया कप टी-ट्वेंटी स्पर्धेचे आयोजन करण्यात आले असून २४ फेब्रुवारी ते ६ मार्च दरम्यान ही स्पर्धा होणार आहे. 11:46AM - नगरमध्ये त्रिमूर्ती कॉलेजमधील १२ वीच्या विद्यार्थीनीची आत्महत्या, वर्गातच लावून घेतला गळफास 11:38AM - बँकॉकमधील नृत्यस्पर्धेसाठी मुख्यमंत्र्याकडून देण्यात आलेला ८ लाख रुपयांचा निधी कलावंतांनी परत दिला आहे.11:50AM - बांगलादेशमध्ये सलग तिसऱ्या वर्षी आशिया कप टी-ट्वेंटी स्पर्धेचे आयोजन करण्यात आले असून २४ फेब्रुवारी ते ६ मार्च दरम्यान ही स्पर्धा होणार आहे. 11:46AM - नगरमध्ये त्रिमूर्ती कॉलेजमधील १२ वीच्या विद्यार्थीनीची आत्महत्या, वर्गातच लावून घेतला गळफास 11:38AM - बँकॉकमधील नृत्यस्पर्धेसाठी मुख्यमंत्र्याकडून देण्यात आलेला ८ लाख रुपयांचा निधी कलावंतांनी परत दिला आहे.11:50AM - बांगलादेशमध्ये सलग तिसऱ्या वर्षी आशिया कप टी-ट्वेंटी स्पर्धेचे आयोजन करण्यात आले असून २४ फेब्रुवारी ते ६ मार्च दरम्यान ही स्पर्धा होणार आहे. 11:46AM - नगरमध्ये त्रिमूर्ती कॉलेजमधील १२ वीच्या विद्यार्थीनीची आत्महत्या, वर्गातच लावून घेतला गळफास 11:38AM - बँकॉकमधील नृत्यस्पर्धेसाठी मुख्यमंत्र्याकडून देण्यात आलेला ८ लाख रुपयांचा निधी कलावंतांनी परत दिला आहे.11:50AM - बांगलादेशमध्ये सलग तिसऱ्या वर्षी आशिया कप टी-ट्वेंटी स्पर्धेचे आयोजन करण्यात आले असून २४ फेब्रुवारी ते ६ मार्च दरम्यान ही स्पर्धा होणार आहे. 11:46AM - नगरमध्ये त्रिमूर्ती कॉलेजमधील १२ वीच्या विद्यार्थीनीची आत्महत्या, वर्गातच लावून घेतला गळफास 11:38AM - बँकॉकमधील नृत्यस्पर्धेसाठी मुख्यमंत्र्याकडून देण्यात आलेला ८ लाख रुपयांचा निधी कलावंतांनी परत दिला आहे.11:50AM - बांगलादेशमध्ये सलग तिसऱ्या वर्षी आशिया कप टी-ट्वेंटी स्पर्धेचे आयोजन करण्यात आले असून २४ फेब्रुवारी ते ६ मार्च दरम्यान ही स्पर्धा होणार आहे. 11:46AM - नगरमध्ये त्रिमूर्ती कॉलेजमधील १२ वीच्या विद्यार्थीनीची आत्महत्या, वर्गातच लावून घेतला गळफास 11:38AM - बँकॉकमधील नृत्यस्पर्धेसाठी मुख्यमंत्र्याकडून देण्यात आलेला ८ लाख रुपयांचा निधी कलावंतांनी परत दिला आहे.11:50AM - बांगलादेशमध्ये सलग तिसऱ्या वर्षी आशिया कप टी-ट्वेंटी स्पर्धेचे आयोजन करण्यात आले असून २४ फेब्रुवारी ते ६ मार्च दरम्यान ही स्पर्धा होणार आहे. 11:46AM - नगरमध्ये त्रिमूर्ती कॉलेजमधील १२ वीच्या विद्यार्थीनीची आत्महत्या, वर्गातच लावून घेतला गळफास 11:38AM - बँकॉकमधील नृत्यस्पर्धेसाठी मुख्यमंत्र्याकडून देण्यात आलेला ८ लाख रुपयांचा निधी कलावंतांनी परत दिला आहे.

    i am using cocur/slugify in laravel 5.1 so It fail at this line for above msg preg_replace($this->regExp, $separator, $string);

    opened by sharvarilimbkar 9
  • Remove rules from Slugify and move them into files

    Remove rules from Slugify and move them into files

    There are two reasons for removing the rules from the Slugify class. First of all, as more and more rules are added (which is great, because it makes the library better) the class gets longer and confusing and harder to maintain and extend. The second big reason is that the current system was not very flexible. We have a lot of problems with conflicting rules in different rules (e.g. #56 or #80) and we want to provide an easy way to change the default rules (or language).

    There is a generate-rules.php script that reads the JSON files and updates a DefaultRuleProvider which allows Slugify to still be very fast.

    In addition this PR cleans up a lot of stuff, this is required to keep the code clean, maintainable, readable and testable when we add features in the future.

    The regular expression has been moved into the options array and a new parameter was added to the constructor called provider. A provider is a class that implements RuleProviderInterface, which defines a getRules() method. The method expects a ruleset parameter and the provider should return the rules for the given ruleset. If no provider is given than an instance of DefaultRuleProvider is created. This class is generated based on the content of the JSON files in Resources/rules, however, the provider just provides the rules, they are not activated by default.

    Rulesets (or languages) are activated based on the new rulesets option. Every ruleset mentioned in this array is activated. The order of the array is important, rules in sets mentioned later can overwrite earlier rulesets. By default the following rulesets are activated:

    • default
    • burmese
    • georgian
    • vietnamese
    • ukrainian
    • latvian
    • finnish
    • greek
    • swedish
    • czech
    • arabic
    • turkish
    • polish
    • german
    • russian

    If you need Turkish (ÄA) transliterations instead of German (ÄAE) you have two possibilities:

    A) Pass the rulesets option to the constructor

    $slugify = new Slugify(['rulesets' => [
          'default',
          'burmese',
          'georgian',
          'vietnamese',
          'ukrainian',
          'latvian',
          'finnish',
          'greek',
          'swedish',
          'czech',
          'arabic',
          'polish',
          'german',
          'russian',
          'turkish', // Turkish after German
    ]);
    

    Or B) you can activate the Turkish ruleset after initialisation.

    $slugify = new Slugify();
    $slugify->activateRuleset('turkish');
    

    This would solve the problems described in #56 and #80 and allows for additional, language specific rules. For example, I added the ruleset austrian which replaces ß with sz instead of ss (in German).

    I would appreciate comments and feedback for this PR very much, since a lot of stuff has been changed. We also need to go through the rules currently in default and move them into rulesets for the specific language (e.g., french, or spanish are missing). I am aware that some rules would be duplicated through this, but I think this situation is better than it was in the past.

    opened by florianeckerstorfer 9
  • Ease extension of Slugify class

    Ease extension of Slugify class

    I made a few minor changes.

    Changed the visibility of rules and rulesets properties to protected to allow them to be overriden or used in child classes while extending Slugify. Maybe using a getter for the rules property would do the same job, but this was my approach.

    Added a third optional param to the slufigy method with the regular expression to be applied to the string. I made this because I needed to allow other characters in the slug, like dots for filenames. I didn't want My Cool File.zip to become my-cool-file-zip but my-cool-file.zip. I wasn't sure if that argument should be set in the constructor or in this method, but I think this is good enough.

    I was thinking also on giving the option to define if the string should be lowered or not, but I'm not sure what's the best way to do that.

    opened by acelaya 8
  • Symfony TreeBuilder deprecation

    Symfony TreeBuilder deprecation

    Since Symfony 3.0 TreeBuilder requires the root name in the constructor. Symfony 4.2 is marking this behaviour as deprecated and will be removed in 5.0.

    opened by DemigodCode 7
  • Rename Symfony service to use Class name instead of 'cocur_slugify'

    Rename Symfony service to use Class name instead of 'cocur_slugify'

    Using symfony 3.4, I get the following deprecation warning:

    Autowiring services based on the types they implement is deprecated since Symfony 3.3 and won't be supported in version 4.0. You should rename (or alias) the "cocur_slugify" service to "Cocur\Slugify\SlugifyInterface" instead.

    Symfony now expects services to be named according to their fully qualified classname. This should be an easy and backwards compatible fix to make.

    opened by raffomania 7
  • Split DefaultRules into deparate files

    Split DefaultRules into deparate files

    Currently, when you use the default rule provider, it loads into memory fully. This means all the rules are loaded each time, even though it's very unlikely you'll require all of them.

    A better approach would be to generate one file per rule and use that to wire the rules on demand, probably using autoloading.

    I've found this issue using meminfo on my app and it pointed me toward chinese rules which I don't use in my app.

    Example output:

    Path from 0x7fccf18bc600
    +----------------------+
    | Id: 0x7fccf21c24c0   |
    | Type: array          |
    | Size: 72 B           |
    | Is root: No          |
    | Children count: 6933 |
    +----------------------+
             ^          
             |          
          chinese       
             |          
             |          
    +--------------------+
    | Id: 0x7fccf3d3e5a0 |
    | Type: array        |
    | Size: 72 B         |
    | Is root: No        |
    | Children count: 38 |
    +--------------------+
             ^          
             |          
           rules        
             |          
             |          
    +-------------------------------------------------------+
    | Id: 0x7fccf3d3e578                                    |
    | Type: object                                          |
    | Class: Cocur\Slugify\RuleProvider\DefaultRuleProvider |
    | Object Handle: 959                                    |
    | Size: 72 B                                            |
    | Is root: No                                           |
    | Children count: 1                                     |
    +-------------------------------------------------------+
             ^          
             |          
          provider      
             |          
             |          
    +------------------------------+
    | Id: 0x7fccf39ab120           |
    | Type: object                 |
    | Class: Cocur\Slugify\Slugify |
    | Object Handle: 886           |
    | Size: 72 B                   |
    | Is root: No                  |
    | Children count: 3            |
    +------------------------------+
    
    opened by dkarlovi 0
  • improve handling of uppercased german Umlauts (Ä to Ae instead of AE)

    improve handling of uppercased german Umlauts (Ä to Ae instead of AE)

    Why?

    It is much more common that a german word containing uppercased Umlauts will be transformed with a small second character:

    Äpfel -> Aepfel Öl -> Oel

    perhaps this is also the case in other languages. What are your opinions about this ?

    opened by thyseus 1
  • Replace `&` with ` and ` by default

    Replace `&` with ` and ` by default

    Currently, b&c gets slugified to b_c, but I think b_and_c would be better. So what about adding a & line to every language ruleset, replacing it with the word and in that language?

    BTW: @harsain does the same in https://github.com/cocur/slugify/issues/148#issuecomment-278846935

    opened by ThomasLandauer 0
  • Take next character into account for *uppercase* german umlauts

    Take next character into account for *uppercase* german umlauts

    According to https://github.com/cocur/slugify/blob/master/Resources/rules/austrian.json#L3 and https://github.com/cocur/slugify/blob/master/Resources/rules/german.json#L3 "Österreich" will be slugified to "OEsterreich", but it should be "Oesterreich". However, when it's all uppercase "OESTERREICH" is correct (not "OeSTERREICH"). So the perfect (?) solution probably would be to check if the next character is uppercase too, then go for E or e.

    opened by ThomasLandauer 0
  • Updated Nette DI bridge for compatibility with Nette Framework 3

    Updated Nette DI bridge for compatibility with Nette Framework 3

    Hey, I have updated the Nette DI bridge (Cocur\Slugify\Bridge\Nette\SlugifyExtension).

    I do not, however, write tests much and I generally don't use Mockery either, so I was unable to update the test.

    opened by VottusCode 0
Releases(v4.3.0)
Owner
Cocur
Cocur
A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Dec 28, 2022
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Motto: "Every business should have a detection script to detect mobile readers." About Mobile Detect is a lightweight PHP class for detecting mobile d

Şerban Ghiţă 10.2k Jan 4, 2023
A PHP string manipulation library with multibyte support

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM. s('string')->toTitleCase()->ensureRight('y') ==

Daniel St. Jules 2.5k Jan 3, 2023
🉑 Portable UTF-8 library - performance optimized (unicode) string functions for php.

?? Portable UTF-8 Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 474 Dec 22, 2022
🔡 Portable ASCII library - performance optimized (ascii) string functions for php.

?? Portable ASCII Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 380 Jan 6, 2023
PHP library to parse urls from string input

Url highlight - PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input. Features: Replace URLs in string b

Volodymyr Stelmakh 77 Sep 16, 2022
:accept: Stringy - A PHP string manipulation library with multibyte support, performance optimized

?? Stringy A PHP string manipulation library with multibyte support. Compatible with PHP 7+ 100% compatible with the original "Stringy" library, but t

Lars Moelleken 144 Dec 12, 2022
ATOMASTIC 14 Mar 12, 2022
A language detection library for PHP. Detects the language from a given text string.

language-detection Build Status Code Coverage Version Total Downloads Minimum PHP Version License This library can detect the language of a given text

Patrick Schur 738 Dec 28, 2022
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.

DeviceDetector Code Status Description The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv

Matomo Analytics 2.4k Jan 5, 2023
Extensive, portable and performant handling of UTF-8 and grapheme clusters for PHP

Patchwork UTF-8 for PHP Patchwork UTF-8 gives PHP developpers extensive, portable and performant handling of UTF-8 and grapheme clusters. It provides

Nicolas Grekas 80 Sep 28, 2022
Provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a unified way.

String Component The String component provides an object-oriented API to strings and deals with bytes, UTF-8 code points and grapheme clusters in a un

Symfony 1.3k Dec 29, 2022
php-crossplane - Reliable and fast NGINX configuration file parser and builder

php-crossplane Reliable and fast NGINX configuration file parser and builder ℹ️ This is a PHP port of the Nginx Python crossplane package which can be

null 19 Jun 30, 2022
A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

A tiny PHP class-based program to analyze an input file and extract all of that words and detect how many times every word is repeated

Max Base 4 Feb 22, 2022
PHP library to detect and manipulate indentation of strings and files

indentation PHP library to detect and manipulate the indentation of files and strings Installation composer require --dev colinodell/indentation Usage

Colin O'Dell 34 Nov 28, 2022
A PHP class which allows the decoding and encoding of a wider variety of characters compared to the standard htmlentities and html_entity_decode functions.

The ability to encode and decode a certain set of characters called 'Html Entities' has existed since PHP4. Amongst the vast number of functions built into PHP, there are 4 nearly identical functions that are used to encode and decode html entities; despite their similarities, however, 2 of them do provide additional capabilities not available to the others.

Gavin G Gordon (Markowski) 2 Nov 12, 2022
A lightweight php class for formatting sql statements. Handles automatic indentation and syntax highlighting.

SqlFormatter A lightweight php class for formatting sql statements. It can automatically indent and add line breaks in addition to syntax highlighting

Jeremy Dorn 3.9k Jan 1, 2023
ColorJizz is a PHP library for manipulating and converting colors.

#Getting started: ColorJizz-PHP uses the PSR-0 standards for namespaces, so there should be no trouble using with frameworks like Symfony 2. ###Autolo

Mikeemoo 281 Nov 25, 2022
Library for free use Google Translator. With attempts connecting on failure and array support.

GoogleTranslateForFree Packagist: https://packagist.org/packages/dejurin/php-google-translate-for-free Library for free use Google Translator. With at

Yurii De 122 Dec 23, 2022