PHP Quick Convert String to Slug

Published 30th November 2012

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