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, "-");
The slug, created from the above function, has following features:
- Alphabets from A to Z are allowed, either upper case or lower case.
- Numbers from 0 to 9 are allowed.
- Spaces and other characters are converted to dash.
- The slug is converted to lowercase.
- Multiple dashes are converted to single dash.
- All trailing spaces and dashes are trimmed.