MIT License

项目介绍

ausi/slug-generator 是一个托管在 GitHub 上的开源项目,主要使用 PHP 编写,采用 MIT License 许可证,目前获得 799 个 Star、50 次 Fork。

仓库全名
ausi/slug-generator
开源许可证
MIT License
Star 数
799
Fork 数
50
未关闭 Issue
10

README 摘要

Slug Generator Library ====================== This library provides methods to generate slugs for URLs, filenames or any other target that has a limited character set. It’s based on PHPs Transliterator class which uses the data of the [CLDR][] to transform characters between different scripts (e.g. Cyrillic to Latin) or types (e.g. upper- to lower-case or from special characters to ASCII). Usage Installation To…

完整内容见 GitHub 原始 README 或本页 。

常见问题

slug-generator 用什么语言开发?

主要使用 PHP 编写,采用 MIT License 许可证,目前有 799 个 Star。

slug-generator 可以免费商用吗?

仓库采用 MIT License 许可证,可免费使用与修改,商业使用请遵守许可证条款(例如保留版权声明与许可声明)。

如何获取 slug-generator 的源码?

可以直接克隆仓库:git clone https://github.com/ausi/slug-generator.git,默认分支为 master;安装与运行步骤见仓库 README。

slug-generator 最近还在维护吗?

仓库最近一次更新为 2023年9月9日,当前有 10 个未关闭 Issue;Star 数为 799,可作为社区活跃度的参考。

关于

Slug Generator Library for PHP, based on Unicode’s CLDR data

描述取自 GitHub 官方 API,中文内容由 GitRead 中文社区 编辑团队整理。

简要分析

热度分
921
价值分
496
活跃状态
活跃

语言占比

PHP

Release

README

Slug Generator Library

Build Status Coverage Packagist Version Downloads MIT License

This library provides methods to generate slugs for URLs, filenames or any other target that has a limited character set. It’s based on PHPs Transliterator class which uses the data of the CLDR to transform characters between different scripts (e.g. Cyrillic to Latin) or types (e.g. upper- to lower-case or from special characters to ASCII).

Usage

<?php
use Ausi\SlugGenerator\SlugGenerator;

$generator = new SlugGenerator;

$generator->generate('Hello Wörld!');  // Output: hello-world
$generator->generate('Καλημέρα');      // Output: kalemera
$generator->generate('фильм');         // Output: film
$generator->generate('富士山');         // Output: fu-shi-shan
$generator->generate('國語');           // Output: guo-yu

// Different valid character set, a specified locale and a delimiter
$generator = new SlugGenerator((new SlugOptions)
    ->setValidChars('a-zA-Z0-9')
    ->setLocale('de')
    ->setDelimiter('_')
);
$generator->generate('Äpfel und Bäume');  // Aepfel_und_Baeume

Installation

To install the library use Composer or download the source files from GitHub.

composer require ausi/slug-generator

Why create another slug library, aren’t there enough already?

There are many code snippets and some good libraries out there that create slugs, but I didn’t find anything that met my requirements. Options are often very limited which makes it hard to customize for different use cases. Some libs carry large rulesets with them that try to convert characters to ASCII, no one uses Unicode’s CLDR which is the standard for transliteration rules and many other transforms.

But most importantly no library was able to do the “correct” conversions, like Ö-Äpfel to OE-Aepfel for German or İNATÇI to inatçı for Turkish. Because the CLDR transliteration rules are context sensitive they know how to correctly convert to OE-Aepfel instead of Oe-Aepfel or OE-AEpfel. CLDR also takes the language into account and knows that the turkish uppercase letter I has the lowercase form ı instead of i.

Options

All options can be set for the generator object itself new SlugGenerator($options) or overwritten when calling generate($text, $options). Options can by passed as array or as SlugOptions object.

delimiter, default "-"

The delimiter can be any string, it is used to separate words. It gets stripped from the beginning and the end of the slug.

$generator->generate('Hello World!');                         // Result: hello-world
$generator->generate('Hello World!', ['delimiter' => '_']);   // Result: hello_world
$generator->generate('Hello World!', ['delimiter' => '%20']); // Result: hello%20world

validChars, default "a-z0-9"

Valid characters that are allowed in the slug. The range syntax is the same as in character classes of regular expressions. For example abc, a-z0-9äöüß or \p{Ll}\-_.

$generator->generate('Hello World!');                             // Result: hello-world
$generator->generate('Hello World!', ['validChars' => 'A-Z']);    // Result: HELLO-WORLD
$generator->generate('Hello World!', ['validChars' => 'A-Za-z']); // Result: Hello-World

ignoreChars, default "\p{Mn}\p{Lm}"

Characters that should be completely removed and not replaced with a delimiter. It uses the same syntax as the validChars option.

$generator->generate("don't remove");                         // Result: don-t-remove
$generator->generate("don't remove", ['ignoreChars' => "'"]); // Result: dont-remove

locale, default ""

The locale that should be used for the Unicode transformations.

$generator->generate('Hello Wörld!');                        // Result: hello-world
$generator->generate('Hello Wörld!', ['locale' => 'de']);    // Result: hello-woerld
$generator->generate('Hello Wörld!', ['locale' => 'en_US']); // Result: hello-world

transforms, default Upper, Lower, Latn, ASCII, Upper, Lower

Internally the slug generator uses Transform Rules to convert invalid characters to valid ones. These rules can be customized by setting the transforms, preTransforms or postTransforms options. Usually setting preTransforms is desired as it applies the custom transforms prior to the default ones.

How Transform Rules (like Lower or ASCII) and rule sets (like a > b; c > d;) work is documented on the ICU website: http://userguide.icu-project.org/transforms

$generator->generate('Damn 💩!!');                                           // Result: damn
$generator->generate('Damn 💩!!', ['preTransforms' => ['💩 > Ice-Cream']]);  // Result: damn-ice-cream

$generator->generate('©');                                          // Result: c
$generator->generate('©', ['preTransforms' => ['© > Copyright']]);  // Result: copyright
$generator->generate('©', ['preTransforms' => ['Hex']]);            // Result: u00a9
$generator->generate('©', ['preTransforms' => ['Name']]);           // Result: n-copyright-sign

Sponsors

Thanks to Blackfire for sponsoring performance profiling tools for this project.

评论

暂无评论