php: convert string to slug

there’s no standard slug function in php. most softwares nowadays comes with function to slug-ify a string to format seo friendly url. but working on a standalone program, this is what i come up with based on some google search results.

function to_slug($string) {
    return preg_replace('/[^a-z\d-]/', '-', strtolower(trim($string)));
}

Leave a Reply

Your email address will not be published. Required fields are marked *