malenki/slug

创建slug的简单类。

1.0.0 2014-04-26 15:40 UTC

This package is not auto-updated.

Last update: 2024-09-14 15:37:58 UTC


README

带有历史记录和语言转写的slug创建器。

如何安装

您可以克隆此存储库或使用 Composer

{
    "require": {
        "malenki/slug": "dev-master",
    }
}

要求

您必须安装 Intl PHP 扩展mbstring PHP 扩展

如何使用

基本用法

非常简单

use \Malenki\Slug;

$s = new Slug('Some string!');

echo $s->render();
//or
echo $s; // toString available, give "some-string"

添加自定义规则

您可以为替换某些字符添加自己的规则

use \Malenki\Slug;

$s = new Slug('One example string, again!');

$s->rule('!', '-wouah');

echo $s; // "one-example-string-again-wouah"

您可以定义空的slug对象,以便以后使用多个字符串,如果定义规则,则非常有用,可以反复使用这些规则

use \Malenki\Slug;

$s = new Slug();

$s->rule('!', '-wouah')->rule('?', '-huh');
$s->v('Genius!');
echo $s; // "genius-wouah"
$s->v('Genius?');
echo $s; // "genius-huh"

历史记录

默认情况下,Slug会将历史记录保存在运行的脚本上下文中,如果已存在生成的slug,则在其上添加递增的数字

use \Malenki\Slug;

$s = new Slug('one-string');
echo $s; // "one-string"
$s = new Slug('one-string');
echo $s; // "one-string-2"
$s = new Slug('one-string');
echo $s; // "one-string-3"

但您也可以禁用此行为

use \Malenki\Slug;

$s = new Slug('one-string');
echo $s->noHistory(); // "one-string"
$s = new Slug('one-string');
echo $s->noHistory(); // "one-string"
$s = new Slug('one-string');
echo $s->noHistory(); // "one-string"

// or

$s = new Slug();
echo $s->noHistory()->v('one-string'); // "one-string"
echo $s->noHistory()->v('one-string'); // "one-string"
echo $s->noHistory()->v('one-string'); // "one-string"

您也可以使用预定义的slug历史记录,如果例如您在数据库中有大量slug,这非常有用

$s = new Slug();
Slug::history(array('one-string', 'another-one'));
echo $s->v('one-string'); // "one-string-2"

非ASCII字符

使用非英语语言也是可能的

// some french
$s = new Slug('C’est rigolo d’écrire en français !');
echo $s; // "c-est-rigolo-d-ecrire-en-francais"

// some greek
$s = new Slug('Τα ελληνικά σου είναι καλύτερα απο τα Γαλλικά μου!');
echo $s; // "ta-ellenika-sou-einai-kalytera-apo-ta-gallika-mou"

所以,请享受吧!