November 30, 2012

PHP Quick Convert String to Slug

Another quick and simple one:

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

// Input: This is My Title // Returns: this-is-my-title

Looking for more?

Subscribe to my newsletter to get infrequent updates in your inbox. Or follow me on Twitter.