Skip to content

Instantly share code, notes, and snippets.

@tjamps
Last active March 7, 2020 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjamps/2d598b395ff6dea5d05f13484b762509 to your computer and use it in GitHub Desktop.
Save tjamps/2d598b395ff6dea5d05f13484b762509 to your computer and use it in GitHub Desktop.
URL Slugs in PHP (with UTF-8 and Transliteration Support)
<?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