The function uses regular expression that replaces spaces between words with hyphens.

public function create_slug($string) 
$slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
$slug = strtolower($slug);
$slug = preg_replace('/-+/', '-', $slug);
$slug = trim($slug, "-");
return $slug;

The slug, created from the above function, has following features:

  1. Alphabets from A to Z are allowed, either upper case or lower case.
  2. Numbers from 0 to 9 are allowed.
  3. Spaces and other characters are converted to dash.
  4. The slug is converted to lowercase.
  5. Multiple dashes are converted to single dash.
  6. All trailing spaces and dashes are trimmed.