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