Last active
March 7, 2020 10:13
-
-
Save tjamps/2d598b395ff6dea5d05f13484b762509 to your computer and use it in GitHub Desktop.
URL Slugs in PHP (with UTF-8 and Transliteration Support)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function slugify(string $input): string | |
{ | |
$transliterated = transliterator_transliterate('Any-Latin; Latin-ASCII; Lower()', $input); | |
return trim(preg_replace('/[^a-z0-9]+/', '-', $transliterated), '-'); | |
} | |
$string = 'Hello, World!'; | |
var_dump(slugify($string)); // outputs "string(11) "hello-world"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment