Make a URL slug from a title

Turns "My First Post!" into "my-first-post".

PHP · Make a URL slug from a titleBeginner
function slugify(string $text): string
{
    $text = strtolower(trim($text));
    $text = preg_replace('/[^a-z0-9]+/', '-', $text);
    return trim($text, '-') ?: 'item';
}

echo slugify('My First Post!'); // my-first-post
slugseostring