rjds/php-slugify
Composer 安装命令:
composer require rjds/php-slugify
包简介
A PHP library to convert a string into a clean URL-safe lowercase string.
README 文档
README
A PHP library to convert a string into a clean URL-safe lowercase string.
Requirements
| Requirement | Version |
|---|---|
| PHP | 8.1+ |
| ext-intl | * |
Installation
Install from Packagist with Composer:
composer require rjds/php-slugify
Usage
Getting started with php-slugify is simple.
Basic Example
use Rjds\PhpSlugify\SluggerFactory; $slugger = SluggerFactory::create(); // Outputs: hello-world-2026 echo $slugger->slugify('Hello World 2026!');
Customizing the Divider
You can change the default hyphen to any other character:
// Outputs: hello_world_2026 echo $slugger->slugify('Hello World 2026', '_');
Custom Character Mapping
You can pass an associative array of custom character replacements via the mappings parameter. Mappings are applied before transliteration, enabling domain-specific replacements:
// Outputs: tom-and-jerry echo $slugger->slugify('Tom & Jerry', mappings: ['&' => 'and']); // Outputs: contact-at-example-dot-com echo $slugger->slugify('contact@example.com', mappings: ['@' => ' at ', '.' => ' dot ']); // Combine with a custom divider // Outputs: price_10_eur echo $slugger->slugify('Price 10€', divider: '_', mappings: ['€' => ' eur']);
Language-Aware Transliteration
By default, the slugger uses generic Unicode-to-ASCII transliteration. For languages with specific conventions, pass a locale to SluggerFactory::create():
// German: ü → ue, ö → oe, ä → ae, ß → ss $slugger = SluggerFactory::create('de'); // Outputs: ueber-die-bruecke echo $slugger->slugify('Über die Brücke'); // Outputs: strasse echo $slugger->slugify('Straße');
// Turkish: ı → i, İ → I, ş → s, ç → c, ğ → g $slugger = SluggerFactory::create('tr'); // Outputs: istanbul echo $slugger->slugify('İstanbul');
Supported locales: de (German), tr (Turkish).
User-provided mappings override locale mappings when both define the same character:
$slugger = SluggerFactory::create('de'); // Outputs: u-boot (user mapping Ü → U overrides locale Ü → Ue) echo $slugger->slugify('Ü-Boot', mappings: ['Ü' => 'U']);
Max Length / Truncation
You can limit the slug length with the maxLength parameter. The slug is truncated on a word boundary so words are never cut in half:
// Outputs: the-quick (truncated at word boundary, not "the-quick-bro") echo $slugger->slugify('The Quick Brown Fox', maxLength: 14); // Outputs: hello-world (no truncation needed) echo $slugger->slugify('Hello World', maxLength: 50); // Combine with a custom divider // Outputs: hello_world echo $slugger->slugify('Hello World 2026', '_', maxLength: 15);
Empty Input Handling
By default, an empty string input returns an empty string. You can customize this with the emptyValue parameter:
// Outputs: (empty string) echo $slugger->slugify(''); // Outputs: n-a echo $slugger->slugify('', emptyValue: 'n-a');
rjds/php-slugify 适用场景与选型建议
rjds/php-slugify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 rjds/php-slugify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rjds/php-slugify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-21