treehouselabs / slugifier
Slugifier
Installs: 200 733
Dependents: 0
Suggesters: 0
Security: 0
Stars: 6
Watchers: 10
Forks: 2
Open Issues: 0
Requires
- php: >=5.4
- ext-iconv: *
- ext-mbstring: *
Requires (Dev)
- phpunit/phpunit: ~4.1
This package is auto-updated.
Last update: 2024-12-17 10:36:28 UTC
README
Simple, extensible library that converts a string to a slug.
Another one?
Yep, sorry. We think this one is pretty good though. Check out the features to see if it's good for you, too.
Features
- Lightweight: no dependencies, other than the
mbstring
andiconv
extensions, which are almost always present. - Easy to use:
(new Slugifier())->slugify('Look Ma, no hands!'); // look-ma-no-hands
- Handles special characters, all kinds of quotes, transliteration, HTML entities, and more. All out of the box.
- Extensible: add your custom translations
- PSR-4 compatible and written using PRS-2 coding guidelines
- Well-tested
Usage
$slugifier = new Slugifier(); $slugifier->slugify('Look má, I\'m a „slug”'); // look-ma-im-a-slug
Use a different delimiter:
$slugifier = new Slugifier(); $slugifier->slugify('Foo, bar & baz', '_'); // foo_bar_baz
Add special translations:
$slugifier = new Slugifier(); $slugifier->addTranslator(new EnglishTranslator()); $slugifier->slugify('Cow & chicken'); // cow-and-chicken
Or write your own:
class CowTranslator implements TranslatorInterface { public function translate($str) { return str_ireplace('cow', 'supercow', $str); } } $slugifier = new Slugifier(); $slugifier->addTranslator(new CowTranslator()); $slugifier->slugify('Cow to the rescue'); // supercow-to-the-rescue
Changing the default behaviour
By default, special characters, quotes, HTML entities and more are converted.
You can disable this behaviour by passing false
to the constructor. This way
you can start from scratch and add your own translators. The iconv
transliteration is still performed though as we want to convert to ascii while
preserving as much of the original data as possible.