Written by Sean Behan on Thu Oct 26th 2017

Here is a snippet for generating a URL friendly slug in PHP

function slugify($string){
        return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string), '-'));
    }

And you can use it in your code like so

<?php echo slugify('!hello world!? how are you???'); // => hello-world-how-are-you ?>

Tagged with..
#php #snippets #slugs #friendly urls

Just finishing up brewing up some fresh ground comments...