PHP code example of cocur / slugify

1. Go to this page and download the library: Download cocur/slugify library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cocur / slugify example snippets


use Cocur\Slugify\Slugify;

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

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

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

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

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

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

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

$slugify = new Slugify([
    "regexp" => "/(?<=[[:^upper:]])(?=[[:upper:]])/",
    "lowercase_after_regexp" => false,
]);
$slugify->slugify("FooBar"); // -> "foo-bar"

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

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

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

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

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

// app/AppKernel.php

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

// config/bundles.php

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

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

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

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

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

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

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

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

use Cocur\Slugify\Slugify;

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

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

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

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

return [
    //...

    "modules" => [
        "Application",
        "ZfcBase",
        "Cocur\Slugify\Bridge\ZF2", // <- Add this line
        //...
    ],

    //...
];

/** @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", "_");

 echo $this->slugify("Hällo Wörld"); 

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

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");
    }
}

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

$latte = new Latte\Engine();
$latte->addFilter("slugify", [new SlugifyHelper(Slugify::create()), "slugify"]);

$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;
};

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);

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);

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);