A URL slug creation and sanitisation library for PHP, simply feed it a string (such as a blog title) and it will create a URL friendly slug.
This library is developed for PHP 5.3+
This library is unit tested against PHP 5.6, 7.0, 7.1, 7.2, 7.3 and 7.4!
This client library is released under the MIT license, a copy of the license is provided in this package.
I highly recommend the use of Composer when installing and using this library, it is not mandatory however and you can use a provided 'include' script to load in this library if required.
Add this dependency to your project by running:
composer require ballen/sluginator
You can use the library "standalone" by downloading it from the GitHub releases section, extracting the files to a place on your server and then adding the "include" into your code like so:
use Ballen\Sluginator\Slug; $slug = new Slug('My awesomely *wickid* example title!!!'); echo $slug;
The output will be
The library also contains other useful methods to enable you to:
- Specify characters of which should be removed from the string, by default a healthy set of non-url characters are removed.
- Specify a different space replacement character (by default this is a hyphen '-').
- Output the slug with or without URL encoding (disabled by default as not required).
- Force output as all lowercase character (default is 'true').
Here is an example using a few extra settings:
use Ballen\Sluginator\Slug; $slug = new Slug('My awesomely *wickid* example title!!!'); $slug->setUseLowercase(false)->setSpaceCharacter('_')->build(); echo $slug->getSlug();
In the above example we have request that the slug character case remains untouched and instead of splitting spaces with a hyphen (-) we will instead replace them with underscore characters (_) instead.
Tests and coverage
This library is fully unit tested using PHPUnit.
I use TravisCI for continuous integration, which triggers the unit tests each time a commit is pushed.
If you wish to run the tests yourself you should run the following:
# Install the Sluginator Library with the 'development' packages this then including PHPUnit! composer install # Now we run the unit tests (from the root of the project) like so: ./vendor/bin/phpunit
Code coverage can also be ran but requires XDebug installed...
./vendor/bin/phpunit --coverage-html ./report
I am happy to provide support via. my personal email address, so if you need a hand drop me an email at: firstname.lastname@example.org.