php: convert string to slug

by prettyscripts on 2010-09-29 12:10

php

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.

PHP:

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